复选框未显示选中 Rails

发布于 2024-10-27 10:42:59 字数 794 浏览 3 评论 0原文

我有一个接受子属性的父模型。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2024-11-03 10:42:59

您需要引用您正在呼叫的用户内的特定spec 记录。尝试更改

<% fields_for "user[spec_attributes]" do |spec_form|%>

<% fields_for @user.spec do |spec_form|%>

您需要确保在编辑控制器操作中为用户构建了一个非零规范对象(但不一定保存)。

You need to reference the specific spec record that is within the user you're calling. Try changing

<% fields_for "user[spec_attributes]" do |spec_form|%>

to

<% fields_for @user.spec do |spec_form|%>

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.

私野 2024-11-03 10:42:59

您可以使用两个 fields_for 调用来执行属性和嵌套属性,如下所示:

<%= form_tag(action) do %>
  ... other form tags ...
  <%= fields_for :user, @user do |f| %>

    <%= f.text_field :first_name %>
    <%= f.text_field :last_name %>

    <%= f.fields_for :spec do |s| %>
      <%= s.check_box :alert_greeting %> 
      <%= s.label :alert_greeting, "Email me when new greetings are posted" %> 
    <% end %>
  <% end %>
<% end %>

You can do attributes and nested attributes using two fields_for calls like this:

<%= form_tag(action) do %>
  ... other form tags ...
  <%= fields_for :user, @user do |f| %>

    <%= f.text_field :first_name %>
    <%= f.text_field :last_name %>

    <%= f.fields_for :spec do |s| %>
      <%= s.check_box :alert_greeting %> 
      <%= s.label :alert_greeting, "Email me when new greetings are posted" %> 
    <% end %>
  <% end %>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文