2010-10-08 120 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的神奇表单助手时,你必须构建表单的其余部分。