2010-10-08 116 views
0

我有一個用於創建/編輯事件的窗體。有一個客戶端下拉菜單,根據所選客戶端呈現一些自定義問題和答案。不保存嵌套屬性

我在Windows上運行Ruby 1.8.6和Mongrel的Rails 2.3.9。 下面是相關代碼:

活動形式

- form_for @event do |f| 
    .... 
    = f.collection_select(:client_id, Client.all, :id, :name) 

    #custom_client_fields 
     = render 'client_fields' if @client 

    = observe_field :event_client_id, :url => {:action => 'client_fields'}, 
        :with => "'client_id=' + encodeURIComponent(value)" 
    .... 

_client_fields.html.haml

- fields_for "event[client_attributes]", @client do |client| 
    - client.fields_for :questions do |question| 
    %label.question= question.object.content 
    - question.fields_for :answers, Answer.new do |answer| 
     = answer.text_field:content, :maxlength => 150, :size => 40 

事件控制器

def client_fields 
    if params[:client_id].blank? 
    render_no_client_fields #self explanatory 
    else 
    respond_to do |format| 
     format.js { 
     render :update do |page| 
      page[:custom_client_fields].replace_html :partial => 'client_fields', :layout => false 
     end 
     } 
    end 
    end 
end 

參數哈希

Parameters: { 
    "event"=>{ 
    "id"=>"2", 
    "client_attributes"=>{ 
     "questions_attributes"=>{ 
     "0"=>{ 
      "id"=>"4", 
      "answers_attributes"=>{ 
      "0"=>{ 
       "content"=>"fjhkghjkk" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

基本上,表單傳遞驗證和除嵌套屬性以外的所有內容。數據庫表中沒有插入任何內容。

望着參數哈希我client_attributes沒有一個id ...嗯...

+0

您使用的是哪種版本的導軌? – Jacob 2010-10-10 21:08:47

回答

2

在局部的client_fields我不得不添加以下代碼來設置client_attributes_id:

<input id="event_client_attributes_id" name="event[client_attributes][id]" type="hidden" value="#{@client.id}"> 

注意自我:當你不使用Rails的神奇表單助手時,你必須構建表單的其餘部分。