2011-05-17 76 views
1

我有这种注册用于与色器件:两个形式的值不被保存

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 

    <p><%= f.label :email %><br /> 
    <%= f.email_field :email %></p> 

    <p><%= f.label :password %><br /> 
    <%= f.password_field :password %></p> 

    <p><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></p> 

    <%= f.fields_for :profile, resource.build_profile do |t| %> 
    <div class ="field"> 
     <%= t.label :type, "Are you an artist or listener?" %><br /> 
     <div>Artist: <%= t.radio_button :type, "Profile::Artist", :id => "artist_button" %></div> 
     <div>Listener: <%= t.radio_button :type, "Profile::Listener", :id => "listener_button" %></div> 
     </div> 
    <% end %> 

    <p class="before"><%= f.submit "Sign up", :id => "new_user_submit" %></p> 
<% end %> 

然后,我有一些JQuery的那个插入动态字段:

$("#artist_button").click(function() { 
     if ($('input#user_name').parent().hasClass('field')){ 
      $('input#user_name').parent().remove(); 
     } 
     $(".before").before('<div class="field"><label for="user_name">Artist or Band/Group Name</label><br><input id="user_name" name="user[name]" size="30" type="text"></div>'); 
    }); 

然而,type属性,该属性是嵌套在用户表单中,并且动态插入的name属性不会保存到数据库中。值被输入为空。

这是为什么?我怎样才能解决这个问题?

UPDATE:

name属性不attr_accessible,并且是导致它不会被保存到数据库。但是,嵌套在用户表单中的type属性为attr_accessible,但仍未保存到数据库中。

+0

实际提交了什么?你在开发日志中看到什么参数? – Roman 2011-05-17 22:29:42

+2

他们是'attr_accessible'吗? – apneadiving 2011-05-17 22:30:43

+0

它在参数中,但我不认为它们是'attr_accessible' ...会输入配置文件模型中? – 2011-05-17 22:32:10

回答

0

尝试把你的用户模型:

attr_accessible :profiles_attributes 

与图谋attr_accessible(email,remeber_me,etc)其他地方一样,不删除你有什么!

0

type一个关联吗?如果是这样,你也必须添加:type_idattr_accessible

0

你有没有在您的用户模型添加

accepts_nested_attributes_for :profile 

1

“type”是一个用于单表继承的“魔术”ActiveRecord列名,这意味着您刚刚通过convention-over-configuration命中。您需要将您的“类型”列重命名为其他内容 - 我通常使用“kind”作为列名称。