Rails 4带有fields_for的嵌套属性不保存到数据库

时间:2014-04-29 19:33:00

标签: checkbox ruby-on-rails-4 nested-attributes strong-parameters fields-for

我想使用accepts_nested_attributes_for通过一个表单在两个不同的表(场地和停车场)上创建记录。我希望用户能够创建一个新的场地,并通过复选框指定该场地可用的停车选项。当我提交表单时,会创建包含模型(场所)的记录,但嵌套模型(停放)没有任何反应。当我检查来自服务器的响应时,我发现我遇到了"未经许可的参数:parking_attributes,"虽然我不确定为什么。

我已观看Railscast #196 Nested Model Form,并尝试了多个stackoverflow帖子(Rails 4 nested attributes not savingRails 4: fields_for in fields_forRails 4 - Nested models(2 levels) not saving)中的建议。如果有人可以帮助我,我会非常感激。

我已经包含了两个模型,场地控制器,场地/新视图以及服务器的响应。

venue.rb

class Venue < ActiveRecord::Base
  has_many :parkings
  accepts_nested_attributes_for :parkings
end

parking.rb

class Parking < ActiveRecord::Base
  belongs_to :venue
end

venues_controller.rb

class VenuesController < ApplicationController
  def index
    @venues = Venue.all
  end

  def new
    @venue = Venue.new
  end

  def create
    @venue = Venue.new(venue_params)
    if @venue.save
      redirect_to @venue, flash: { success: "Venue successfully created" }
    else 
      render :new
    end
  end

  def show
    @venue = Venue.find(params[:id])
  end

  def edit
    @venue = Venue.find(params[:id])
  end

  def update
    @venue = Venue.find(params[:id])
    if @venue.update(venue_params)
      redirect_to @venue
    else
      render "edit"
    end
  end

  def destroy
    @venue = Venue.find(params[:id])
    if @venue.destroy
      redirect_to venues_path, flash: { success: "Venue successfully destroyed" }
    else
      render "show", flash: { error: "Venue was not successfully destroyed" }
    end
  end

private

  def venue_params
    params.require(:venue).permit(
      :name,:address,:city,:state,:zip,
      parking_attributes: [:id, :venue_id, :none, :street_free])
  end

end

/venues/new.haml

%h1 Add a new venue
= form_for @venue do |f|
  = f.label :name
  = f.text_field :name
  = f.label :address
  = f.text_field :address
  = f.label :city
  = f.text_field :city
  = f.label :state
  = f.text_field :state
  = f.label :zip
  = f.text_field :zip

  = f.fields_for :parkings do |p|
    = p.label :none
    = p.check_box :none
    = p.label :street_free
    = p.check_box :street_free
  = f.submit

服务器响应

Started POST "/venues" for 127.0.0.1 at 2014-04-29 14:02:54 -0500
Processing by VenuesController#create as HTML
  Parameters: {"utf8"=>"✓",
    "authenticity_token"=>"kMcVVwXq7f22rIGm1rQ6+QzC80ScmXrVA2IE8TGbN7w=", 
    "venue"=>{"name"=>"The Five O'Clock Lounge", 
    "address"=>"11904 Detroit Ave", 
    "city"=>"Lakewood", 
    "state"=>"OH", 
    "zip"=>"44107", 
    "parkings_attributes"=>
      {"0"=>
        {"none"=>"1",
          "street_free"=>"0"
        }
      }
    }, 
    "commit"=>"Create Venue"}
Unpermitted parameters: parkings_attributes
  (0.2ms)  BEGIN
SQL (107.0ms)  INSERT INTO "venues" (
  "address", 
  "city", 
  "created_at", 
  "name", "state", 
  "updated_at", "zip"
) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  
  [
    ["address", "11904 Detroit Ave"], 
    ["city", "Lakewood"], 
    ["created_at", Tue, 29 Apr 2014 19:02:54 UTC +00:00], 
    ["name", "The Five O'Clock Lounge"], 
    ["state", "OH"], 
    ["updated_at", Tue, 29 Apr 2014 19:02:54 UTC +00:00], 
    ["zip", 44107]
  ]
SQL (47.5ms)  INSERT INTO "parkings" (
  "created_at", 
  "updated_at", 
  "venue_id") VALUES ($1, $2, $3) RETURNING "id"  
    [
      ["created_at", Tue, 29 Apr 2014 19:02:54 UTC +00:00], 
      ["updated_at", Tue, 29 Apr 2014 19:02:54 UTC +00:00], 
      ["venue_id", 10]
    ]
  (0.6ms)  COMMIT
Redirected to http://localhost:3000/venues/10
Completed 302 Found in 165ms (ActiveRecord: 155.2ms)

更新:已解决

根据Kirti的建议,我能够超越未经许可的参数错误。

1 个答案:

答案 0 :(得分:8)

更新venue_params方法如下:

 def venue_params
   params.require(:venue).permit(
      :name,:address,:city,:state,:zip,
      parkings_attributes: [:id, :venue_id, :none, :street_free])
 end

请注意parkings_attributes (多个停车场)而非parking_attributes (单一停车场)

如果您在VenueParking型号之间存在 1-M关系,您将收到parkings_attributes (多个停车场) params哈希但是您当前的venue_params代码已列入白名单parking_attributes (单一停车位)。这导致警告Unpermitted parameters: parkings_attributes