我的 Create 控制器将嵌套资源的子项作为 null 传递,而不是传递我刚刚输入到表单中的值。
这是我的代码
路线:
resources :trips do
resources :pilgrims
end
楷模:
旅行:
class Trip < ActiveRecord::Base
has_many :pilgrims
accepts_nested_attributes_for :pilgrims, :allow_destroy => true
attr_accessible :end_date, :leader_id, :name, :start_date, :pilgrim_attributes
end
朝圣:
class Pilgrim < ActiveRecord::Base
belongs_to :trip
attr_accessible :pilgrim_id, :surname, :name, :middle, :aka, :prefix, :address, :city, :state, :zip, :email, :telephone, :nationality, :date_of_birth, :passport_number, :expiration, :jordan, :room, :price, :status, :trip_id
end
我的朝圣者控制器:
def new
@trip = Trip.find(params[:trip_id])
@pilgrim = Pilgrim.new
end
def create
@trip = Trip.find(params[:trip_id])
@pilgrim = @trip.pilgrims.build(params[:pilgrim])
if @pilgrim.save
flash[:notice] = "The <b>#{ @pilgrim.name }</b> has been created successfully."
redirect_to(trip_pilgrims_path, :notice => "The <b>#{ @pilgrim.name }</b> ship has been saved successfully.")
else
render(:new, :error => @pilgrim.errors)
end
end
使用我的表单代码表单链接到要点
路线似乎是正确的,当我单击 new_trip_pilgrim_path(@trip) 时,它确实指向 trips/:trip_id/pilgrims/new 并加载新的朝圣者表格。
但是,当我单击表单上的保存时,它会将我重定向到路线 trips/3/pilgrims,但会显示新的 pilgrim 表单,说所有必填字段都留空。
这是日志中显示的内容。
Started POST "/trips/3/pilgrims" for 127.0.0.1 at 2013-01-19 22:12:06 -0800
Processing by PilgrimsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"kOE06m3DNax43BOLYZ6t1lS7/T4wOWb2xM8m/mlQzvA=", "commit"=>"Create Pilgrim", "trip_id"=>"3"}
Trip Load (0.3ms) SELECT `trips`.* FROM `trips` WHERE `trips`.`id` = 3 LIMIT 1
(0.2ms) BEGIN
(0.2ms) ROLLBACK
Pilgrim Load (0.4ms) SELECT `pilgrims`.* FROM `pilgrims` WHERE `pilgrims`.`trip_id` = 3
Rendered pilgrims/_form.html.erb (36.4ms)
Rendered pilgrims/new.html.erb within layouts/application (37.2ms)
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
Trip Load (0.3ms) SELECT `trips`.* FROM `trips`
Completed 200 OK in 244ms (Views: 102.5ms | ActiveRecord: 11.2ms)
Pilgrim 创建控制器发生了什么?