Rails 3 接受嵌套属性不起作用
我尝试在用户模型上使用嵌套属性进行设置,以便我可以在单个页面中编辑用户信息和设置信息。这是我的代码:
User.rb
has_one :settings
accepts_nested_attributes_for :settings, :allow_destroy => true
attr_accessor :settings_attributes
views/users/registrations/edit.html.erb
<%= f.fields_for :settings do |s| %>
<p>
<%= s.label :newsletter %><br />
<%= s.check_box :newsletter %>
</p>
<% end %>
这看起来工作正常,表单显示正确。但是,当我提交表单时,设置的属性根本没有改变。
我正在使用 Rails 3,Devise 创建了我的用户模型。也许我遗漏了控制器模型中需要的东西?
I am trying to use nested attributes on my user model for settings so that I can edit the user info and the setting info in a single page. Here is my code:
User.rb
has_one :settings
accepts_nested_attributes_for :settings, :allow_destroy => true
attr_accessor :settings_attributes
views/users/registrations/edit.html.erb
<%= f.fields_for :settings do |s| %>
<p>
<%= s.label :newsletter %><br />
<%= s.check_box :newsletter %>
</p>
<% end %>
This seemingly works just fine, the form is displayed properly. However the attributes for the settings are not changed at all when I submit the form.
I am using Rails 3, and Devise created my user model. Perhaps I am missing something that needs to go in the controller model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最有可能是由于使用复数而不是单数进行设置造成的。尝试更改为:
模型
视图
您使用 attr_accessor 做什么?
Most likely caused by using plural instead of singular for setting. Try changing to this:
model
view
What are you using the attr_accessor for?