Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 具有关联的嵌套模型/表单_Ruby On Rails_Nested Forms_Nested Attributes - Fatal编程技术网

Ruby on rails 具有关联的嵌套模型/表单

Ruby on rails 具有关联的嵌套模型/表单,ruby-on-rails,nested-forms,nested-attributes,Ruby On Rails,Nested Forms,Nested Attributes,我有以下三种关系模型: # category.rb class Category < ActiveRecord::Base has_many :category_marks accepts_nested_attributes_for :category_marks end # category_mark.rb class CategoryMark < ActiveRecord::Base attr_accessible: :add, :stu, :inc vali

我有以下三种关系模型:

# category.rb
class Category < ActiveRecord::Base
  has_many :category_marks
  accepts_nested_attributes_for :category_marks
end

# category_mark.rb
class CategoryMark < ActiveRecord::Base
  attr_accessible: :add, :stu, :inc

  validates_presence_of :user_group
  validates_presence_of :category_id
  belongs_to :category
  belongs_to :user_group
end

# user_group.rb
class UserGroup < ActiveRecord::Base
  has_many :category_marks
end
每个类别都需要为每个用户组创建一个类别标记,因此在
new
操作中,我将在新的
@category
对象上为每个用户组创建一个类别标记对象和关联。这很好,使我能够使用嵌套表单显示每个类别标记的表单字段。我现在遇到的困难是在
create
操作中保存这些类别标记

params
进入
create
类似:

{"utf8"=>"✓",
"authenticity_token"=>"0rNpYy7OB+DPuzmu8HoxX2MvTnkjUyU+Vej+a4RMh7s=",
"category"=>{
  "name"=>"test category",
  "color"=>"#00f",
  "parent_id"=>"3",
  "category_marks_attributes"=>{
    "0"=>{
      "stu"=>"1",
      "inc"=>"0",
      "add"=>"1"},
    "1"=>{
      "stu"=>"0",
      "inc"=>"1",
      "add"=>"1"}}},
"button"=>"",
"action"=>"create",
"controller"=>"admin/categories"}
查看表单的代码:

<%= form_for([:admin, @category]) do |category_form| %>
  <ul>
    <li><%= category_form.label :name %> <%= category_form.text_field :name %></li>
    <li><%= category_form.label :color %> <%= category_form.text_field :color, class: :add_colour_picker %></li>
    <li><%= category_form.label :parent_id, "Parent category" %> <%= category_form.select :parent_id, Category.roots.delete_if{|c| c == @category}.map{|c| [c.name, c.id]}, include_blank: true %></li>
    <br />
    <h2>User Group Permissions</h2>
    <table>
      <tr>
        <th>User Group</th>
        <th>View Students</th>
        <th>View Incidents</th>
        <th>Add Incidents</th>
      </tr>
      <%= category_form.fields_for :category_marks do |category_marks_form| %>
      <%= category_marks_form.hidden_field :user_group_id, value: category_marks_form.object.user_group_id %>
        <tr>
          <td><%= category_marks_form.object.user_group.name %></td>
          <td><%= category_marks_form.check_box :stu %></td>
          <td><%= category_marks_form.check_box :inc %></td>
          <td><%= category_marks_form.check_box :add %></td>
        </tr>
      <% end %>
    </table>

    <li><%= button_tag "Save Category" %></li>
  </ul>
<% end %>


  • 用户组权限 用户组 查看学生 查看事件 添加事件

您的
参数中的
类别标记\u属性
缺少
用户组\u id
,因此不会根据用户输入进行验证或保存

您可能需要在视图中添加以下内容:

= category_marks_builder.hidden_field :group_id, value: category_marks_builder.object.group_id
另外,我很确定它的工作原理与您的相同,但我会去掉
@category.attributes=
行,并调用
@category.update\u attributes params[:category]
,而不是
@category.save

最后,我不确定您为什么要在
创建
控制器中再次执行
@category.category\u marks.build
调用。我想这将为您提供两套
类别标记
。一个是用户输入,一个是默认值


希望能有帮助。

谢谢你的帮助!我仍然无法让它工作,错误如下:
#[“不能为空”],:“category\u marks.category”=>[“不能为空”]}>
。我已经用视图代码更新了我的问题。如果您将
验证
语句的存在以包括或排除
\u id
后缀,它会改变什么吗?我想你可能需要
用户组id
类别
。使用各种组合都没有成功,但我已经从验证和关系中去掉了
\u id
,因为我认为这是不必要的,也不容易混淆。我现在只得到一个错误:
:“category\u marks.category”=>[“不能为空”]
-argh!我已经尝试将
类别
类别id
添加到
类别标记
属性
,但都没有解决。啊,等一下,如果我拿出我的
验证:类别
的存在,效果非常好。它似乎在检查类别是否存在,甚至在它保存类别之前,hmpf。现在我想起来了,我想起来了。我想你可能需要一个与
相反的语句。
= category_marks_builder.hidden_field :group_id, value: category_marks_builder.object.group_id