控制器不将值从from传递给create方法

时间:2013-01-20 06:19:30

标签: ruby-on-rails

我的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

我的Pilgrim控制器:

 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

使用我的表单代码Form

链接到一个要点

路线似乎是正确的,当我点击new_trip_pilgrim_path(@trip)时,它确实指向trip /:trip_id / pilgrims / new并加载新的朝圣形式。

然而,当我点击表单上的保存时,它会将我重定向到路线旅行/ 3 /朝圣者,但显示新的朝圣形式,说所有必填字段都留空。

这是日志中显示的内容。

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创建控制器发生了什么?

1 个答案:

答案 0 :(得分:0)

您正在尝试通过调用

创建一个新的朝圣者对象
@pilgrim = @trip.pilgrims.build(params[:pilgrim])
PilgrimsController中的

。但是Parameters: {"utf8"=>"✓", "authenticity_token"=>"kOE06m3DNax43BOLYZ6t1lS7/T4wOWb2xM8m/mlQzvA=", "commit"=>"Create Pilgrim", "trip_id"=>"3"}不包含任何朝圣者的参数。所以问题在于用于创建新朝圣者的形式