2012-09-30 63 views
0

应用程序/控制器/ bookings_controller.rb找不到约会:45:在'创造”的ActiveRecord :: RecordNotFound在BookingsController#创建没有ID

我有一个约会对象和预订对象。预订属于约会和约会has_many预订。

我想创建一个具有预约对象:appointment_id

这里是我的代码:

<% @appointments.each do |appointment| %> 
    <%= link_to "new Booking", new_appointment_booking_path(appointment)%> 
<%end%> 

预订控制器:

def new 
    @appointment = Appointment.find(params[:appointment_id]) 
    @booking = @appointment.bookings.new 
    ... 

def create 
I was missing [:booking] in line 45. 
Line 45: @appointment = Appointment.find(params[:booking][:appointment_id]) 
    @booking = @appointment.bookings.new(params[:booking]) 

路线

resources :appointments do 
    resources :bookings 
end 

瓦恩我提交Bookings_form正确appointment_id通过 通过,但我得到以下错误:

ActiveRecord::RecordNotFound in BookingsController#create 
Couldn't find Appointment without an ID 

预订_form

<%= simple_form_for(@booking) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input :appointment_date %> 
    <%= f.input :start_time %> 
    <%= f.input :end_time %> 
    <%= f.input :name %> 
    <%= f.input :phone_number %> 
    <%= f.hidden_field :appointment_id%> 

<div class="form-actions"> 
    <%= f.button :submit %> 
    </div> 
+0

你的表单是怎样的? – apneadiving

+0

你能告诉控制器哪一行是第45行吗? – rewritten

+0

第45行创建动作:@appointment = Appointment.find(params [:appointment_id]) – Benamir

回答

1

你没有通过预约ID回create方法。

create方法除了通过表单输入中的params散列传入的信息外什么也不知道。由于您尚未向appointment_id的表单中添加字段,因此它没有传递给控制器​​,并且在方法中不可用。

为了解决这个问题,一个新的隐藏输入添加到您的形式是这样的:

<%= f.input :appointment_id, :type => :hidden %> 

现在你明确地传递了ID通过表单提交,所以它会在你的控制器提供。

+0

感谢David的回复。我试过了,并且正在通过appointment_id,但它仍然给我同样的错误。任何想法可能是什么? – Benamir

+0

'appointment_id'参数将在您的'booking'中,您是否试过像这样访问它:'params [:booking] [:appointment_id]'? –

+0

谢谢戴夫!那工作。 – Benamir

相关问题