2012-09-30 48 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

相關問題