两个表单值未保存
我有这样的注册:
<%= 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
属性动态插入并没有保存到数据库中。输入的值为空。
这是为什么呢?我该如何解决这个问题?
更新:
名称属性不是attr_accessible
,这导致它无法保存到数据库中。但是,嵌套在用户表单内的 type 属性是 attr_accessible ,但仍未保存到数据库中。
I have this signup for with devise:
<%= 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 %>
Then I have some JQuery that inserts a field dynamically:
$("#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>');
});
However, the type
attribute that is nested in the user form and the name
attribute that is inserted dynamically are not being saved to the database. The values are being entered as null.
Why is this? How can I fix this?
UPDATE:
The name attribute was not attr_accessible
and that was causing it to not be saved to the db. However, the type attribute, which is nested inside the user form, is attr_accessible
but is still not being saved to the db.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“type”是一个“神奇的”ActiveRecord 列名称,用于单表继承,这意味着您刚刚遇到了约定优于配置的问题。您需要将“type”列重命名为其他名称 - 我通常使用“kind”作为列名称。
"type" is a "magic" ActiveRecord column name used for single table inheritance which means that you just got hit by convention-over-configuration. You need to rename your "type" column to something else - I usually use "kind" as a column name.
尝试放入您的用户模型:
与其余设备一起
attr_accessible(email,remeber_me,etc)
,不要删除您拥有的!Try putting in your User Model:
along with the rest of devises
attr_accessible(email,remeber_me,etc)
, do not delete what you have!type
是关联吗?如果是这样,您还必须将:type_id
添加到attr_accessible
中。Is
type
an association? If so, you'd have to add:type_id
toattr_accessible
, too.您添加了
用户模型吗?
Did you add
in your User model?