复选框未显示选中 Rails
我有一个接受子属性的父模型。
class User < ActiveRecord::Base
accepts_nested_attributes_for :spec
attr_accessible :name, :spec_attributes
在视图中,我有一个获取 3 个模型信息的表单。我使用通用的 form_tag。
<% form_tag(action) do %>
.
.
.
<% fields_for "user[spec_attributes]" do |spec_form|%>
<%= spec_form.check_box :alert_greeting %>
<%= spec_form.label :alert_greeting, "Email me when new greetings are posted" %>
<% end %>
<% end %>
在控制器中,
@user = User.find(session[:user_id])
if @user.update_attributes(params[:user])
do something.
end
数据库正在更新,一切似乎都在工作。
但是,当我返回表单再次编辑时,即使该复选框的值显示 1,该复选框也不会被选中。
关于如何在应有的情况下将复选框显示为已选中的任何想法?
预先非常感谢。
I have a parent model that accepts child attributes.
class User < ActiveRecord::Base
accepts_nested_attributes_for :spec
attr_accessible :name, :spec_attributes
In the view I have a form that gets information for 3 models. I use a generic form_tag.
<% form_tag(action) do %>
.
.
.
<% fields_for "user[spec_attributes]" do |spec_form|%>
<%= spec_form.check_box :alert_greeting %>
<%= spec_form.label :alert_greeting, "Email me when new greetings are posted" %>
<% end %>
<% end %>
In the Controller
@user = User.find(session[:user_id])
if @user.update_attributes(params[:user])
do something.
end
The database is getting updated and the all seems to be working.
However when I go back to the form to edit again, even though the value for the checkbox is showing 1 the check box is not checked.
Any ideas as to how to show the checkbox as being checked when it is supposed to be?
Thanks a lot in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要引用您正在呼叫的用户内的特定
spec
记录。尝试更改为
您需要确保在编辑控制器操作中为用户构建了一个非零规范对象(但不一定保存)。
You need to reference the specific
spec
record that is within the user you're calling. Try changingto
You'll need to make sure that you have a non-nil spec object built for the user (but not necessarily saved) in your edit controller action.
您可以使用两个
fields_for
调用来执行属性和嵌套属性,如下所示:You can do attributes and nested attributes using two
fields_for
calls like this: