Strong Params不会保存嵌套的has_many

时间:2013-12-05 04:04:51

标签: ruby-on-rails-4 nested-forms nested-attributes strong-parameters

我正在使用Rails 4,并使用强大的Params。由于某种原因,我无法保存我的嵌套参数。我已经看过如何做到这一点,甚至可能以前在不同的Rails应用程序中使用它。我知道允许draft_players_attributes并在数组中列出所有已接受的参数。

以下是进入的参数:

Started POST "/draft_groups" for 127.0.0.1 at 2013-12-04 22:55:32 -0500
  User Load (1.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Processing by DraftGroupsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2W3bs1U7+CEzsWl+jDi3xZi5CyldYeZXCz3KU6c+sYY=", "draft_group"=>{"name"=>"Best Group Ever", "draft_id"=>"3", "captain_id"=>"1"}, "draft_players"=>{"player_id"=>"1", "position"=>"Handler", "rating"=>"10", "info"=>"Smart"}, "commit"=>"Update"}
   (0.3ms)  BEGIN
  SQL (2.6ms)  INSERT INTO "draft_groups" ("captain_id", "created_at", "draft_id", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["captain_id", 1], ["created_at", Thu, 05 Dec 2013 03:55:32 UTC +00:00], ["draft_id", 3], ["name", "Best Group Ever"], ["updated_at", Thu, 05 Dec 2013 03:55:32 UTC +00:00]]
   (0.5ms)  COMMIT
Redirected to http://lvh.me:3000/drafts/3
Completed 302 Found in 8ms (ActiveRecord: 3.3ms)

这是我的控制器:

class DraftGroupsController < ApplicationController
  def create
    @draft_group = DraftGroup.create(draft_group_params)
    redirect_to :back, :notice => "Draft Group successfully created."
  end

  def update
    @draft_group = DraftGroup.find(params[:id])
    @draft_group.update(draft_group_params)
    redirect_to :back, :notice => "Draft Group successfully updated."
  end

  def destroy
    @draft_group = DraftGroup.find(params[:id]).destroy
    redirect_to :back, :notice => "Draft Group successfully destroyed."
  end

  private
  def draft_group_params
    params.require(:draft_group).permit(:name,
                                        :draft_id,
                                        :captain_id,
                                        draft_players_attributes:
                                          [
                                            :_destroy,
                                            :id,
                                            :player_id,
                                            :position,
                                            :rating,
                                            :info
                                          ]
                                       )
  end
end

我的模特:

class DraftGroup < ActiveRecord::Base
  has_many :draft_players, :dependent => :destroy
  belongs_to :captain, :class_name => "User"

  accepts_nested_attributes_for :draft_players
end

class DraftPlayer < ActiveRecord::Base
  belongs_to :draft_group
  belongs_to :player, class_name: "User"
end

我的观点:

<% @groups.each do |group| %>
  <div class="span6 group">
    <h4><%= "#{group.name}" %></h4>
    <%= simple_form_for(group, :html => { :class => "auto-width" } ) do |f| %>
      <div class="row">
        <%= f.input :name, :label => false %>
        <%= f.hidden_field :draft_id %>
        <%= f.hidden_field :captain_id %>
      </div>
      <table>
        <tr>
          <th>Player</th>
          <th>Position</th>
          <th>Rating</th>
          <th>Info</th>
        </tr>
        <%= simple_fields_for :draft_players do |player| %>
          <tr>
            <td><%= player.input :player_id, :label => false, :as => :select, :collection => User.active %></td>
            <td><%= player.input :position, :label => false %></td>
            <td><%= player.input :rating, :label => false %></td>
            <td><%= player.input :info, :label => false %></td>
          </tr>
        <% end %>
      </table>
      <div class="row">
        <%= f.button :submit, "Update", :class => "btn btn-primary" %>
      </div>
    <% end %>
  </div>
<% end %>

编辑:添加了视图代码,可能会使用表格,将其转换为使用bootstraps列布局。

1 个答案:

答案 0 :(得分:1)

要确保参数正确嵌套(因此Rails可以理解嵌套的模型属性),您应该调用f.simple_fields_for(:draft_players)而不是simple_fields_for(:draft_players)

换句话说,在FormBuilder对象simple_fields_for上调用f,而不是直接调用帮助程序。

这样Rails就可以正确查找并验证嵌套关联。