我想使用accepts_nested_attributes_for通过一个表单在两个不同的表(场地和停车场(上创建记录。我希望用户能够创建一个新的场地,并通过复选框指定该场地可用的停车选项。当我提交表单时,会创建包含模型(场地(的记录,但嵌套模型(停车场(不会发生任何事情。当我检查服务器的响应时,我发现我遇到了"未经许可的参数:parking_attributes",尽管我不知道为什么。
我看过Railscast#196嵌套模型表单,并尝试了来自多个stackerflow帖子的建议(Rails4嵌套属性未保存,Rails4:fields_for中的fields_for,以及Rails4-嵌套模型(2级(未保存(。如果有人能帮我,我将不胜感激
我已经包含了两个模型,场馆控制器、场馆/新视图和服务器的响应。
venue.rb
class Venue < ActiveRecord::Base
has_many :parkings
accepts_nested_attributes_for :parkings
end
停车.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
/场馆/新建.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的建议,我能够克服未允许的参数错误。
更新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
。
由于Venue
和Parking
模型之间存在1-M关系,您将在params
哈希中收到parkings_attributes
(复数停车(,但在您当前的venue_params
代码中,您将parking_attributes
列入了白名单(单数停车(。这导致警告Unpermitted parameters: parkings_attributes