带有fields_for的Rails 4嵌套属性不保存到数据库 [英] Rails 4 Nested Attributes with fields_for Don't Save to Database
问题描述
我想使用accepts_nested_attributes_for通过一种形式在两个不同的表(场地和停车位)上创建记录.我希望用户能够创建一个新的场所,并通过复选框指定该场所可用的停车选项.当我提交表单时,将创建包含模型(地点)的记录,但是嵌套模型(停放)没有任何反应.当我检查服务器的响应时,虽然不确定为什么,但发现遇到了未经许可的参数:parking_attributes".
I want to create records on two different tables (venue and parking) via one form using accepts_nested_attributes_for. I want a user to be able to create a new venue, and also specify the parking options available to that venue via checkboxes. When I submit the form, the record for the containing model (venue) is created, but nothing happens with the nested model (parking). When I check the response from the server, I see that I'm encountering "Unpermitted parameters: parking_attributes," although I'm not sure why.
我看过 Railscast#196嵌套模型表单,并尝试了多个stackoverflow帖子中的建议(设置4个不保存的嵌套属性, 路线4:fields_for 和
I've watched Railscast #196 Nested Model Form, and tried the suggestions from multiple stackoverflow posts (Rails 4 nested attributes not saving, Rails 4: fields_for in fields_for, and Rails 4 - Nested models(2 levels) not saving). If anybody can help me out, I'd greatly appreciate it.
我包括了两个模型,场所控制器,场所/新视图以及服务器的响应.
I've included the two models, the venues controller, the venues/new view, and the response from the server.
venue.rb
class Venue < ActiveRecord::Base
has_many :parkings
accepts_nested_attributes_for :parkings
end
parking.rb
parking.rb
class Parking < ActiveRecord::Base
belongs_to :venue
end
venues_controller.rb
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
/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 的建议,我能够克服不允许的参数错误.
Following the advice of Kirti, I was able to get past the unpermitted parameters error.
推荐答案
更新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
哈希BUT中收到parkings_attributes
(多个停车位)您当前的venue_params
代码已将parking_attributes
(单个停车位)列入白名单.这引起警告Unpermitted parameters: parkings_attributes
As you have 1-M relationship between Venue
and Parking
model you would receive parkings_attributes
(plural parkings) in params
hash BUT in your current code for venue_params
you whitelisted parking_attributes
(singular parking). This is causing the warning Unpermitted parameters: parkings_attributes
这篇关于带有fields_for的Rails 4嵌套属性不保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!