0

app/controllers/bookings_controller.rb:45:in `create'

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

我想创建一个具有 :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 时,通过了正确的约会 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>
4

1 回答 1

1

您没有将 Appointment id 传递回该create方法。

除了从表单输入通过 params 哈希传入的信息外,该create方法一无所知。看到您没有在表单中添加字段appointment_id,因此它没有传递给控制器​​并且在create方法中不可用。

要解决这个问题,请在表单中添加一个新的隐藏输入,如下所示:

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

现在您通过表单帖子明确传递 id,因此它将在您的控制器中可用。

于 2012-09-30T23:08:45.983 回答