1

我有以下三个具有关系的模型:

# 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

category_marks描述不同用户组对不同类别的访问权限。我在创建新类别时尝试创建类别标记,因此accepts_nested_attributes_for. 我可以同时编辑类别和更新 category_marks,但不是在我创建新类别时。这是我的categories_controller.rb(删除了不相关的位):

class Admin::CategoriesController < ApplicationController

    def new
      @category = Category.new
      @category.category_marks.build(UserGroup.all.map{|ug| { user_group: ug }})

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @category }
      end
    end

    def edit
      @category = Category.find(params[:id])
    end

    def create
      @category = Category.new
      @category.category_marks.build(UserGroup.all.map{|ug| { user_group: ug }})
      @category.attributes = params[:category]

      respond_to do |format|
        if @category.save
          format.html { redirect_to [:admin, :categories], notice: 'Category was successfully created.' }
          format.json { render json: @category, status: :created, location: @category }
        else
          format.html { render action: "new" }
          format.json { render json: @category.errors, status: :unprocessable_entity }
        end
      end
    end

    def update
      @category = Category.find(params[:id])

      respond_to do |format|
        if @category.update_attributes(params[:category])
          format.html { redirect_to [:admin, :categories], notice: 'Category was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @category.errors, status: :unprocessable_entity }
        end
      end
    end
  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 %>
4

1 回答 1

2

category_marks_attributes的 inparams缺少user_group_id,因此不会根据用户输入进行验证或保存。

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

= category_marks_builder.hidden_field :group_id, value: category_marks_builder.object.group_id

另外,我很确定它可以按照您的方式工作,但是我会摆脱@category.attributes=线路并调用@category.update_attributes params[:category]而不是@category.save.

最后,我不确定你为什么要在控制器@category.category_marks.build中再次调用。create我想这会给你两套category_marks. 一种是用户输入,另一种是默认值。

我希望它有所帮助。

于 2013-01-25T17:30:07.683 回答