1

我的模型摘要:用戶有許多約會。預約有很多預訂。預訂屬於預約。路由錯誤:將參數傳遞給控制器​​中的操作

我想鏈接到一個特定的視圖(稱爲「users_bookings」),該視圖列出了特定約會的所有預訂。這是我曾嘗試:

<% current_user.appointments.each do |appointment|%> 
    <%= link_to "view all bookings", users_bookings_appointment_booking_path(appointment)%> 
<%end%> 

這是我的錯誤:

undefined method `users_bookings_appointment_bookings' 

附加信息:

路線:

resources :appointments do 
    resources :bookings do 
     get 'users_bookings', :on => :collection 
    end   
    end 

登記控制器創建行動:

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

登記控制器Users_bookings操作:

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

Users_bookings查看:

<% @bookings.each do |booking| %> 
    <td><%= booking.appointment_date%></td> 
    <td><%= booking.start_time %></td> 
    <td><%= booking.end_time %></td> 
<%end%> 

回答

1

你不應該使用match(如其他人所說),除非你真的想匹配該URL所有 HTTP請求(GETPOST等)。相反,只需添加一個路由到資源的do塊:

resources :appointments do 
    resources :bookings do 
    get 'user_bookings', :on => :collection 
    end 
end 

這將增加一個額外的途徑爲GET請求`/約會/:appointment_id /預訂/ user_bookings'並將其路由到「預訂# user_bookings'。

+0

謝謝。非常感謝幫助。 – Benamir

0

我要改變它是預約控制器的索引行爲。這樣它會匹配'/約會/ 1 /預訂'。

但是,如果有一個原因,你不能這樣做,因爲它不是標準路線之一,你需要在routes.rb文件中指定它。喜歡的東西:

match '/appointments/:id/bookings/users_bookings' => 'bookings#users_bookings' 
+0

謝謝。非常感謝幫助。 – Benamir

0

你試圖定義它指向你的控制器定義動作的自定義路徑,如:

match 'appointments/:id/users_bookings' => 'bookings#users_bookings', :as => :users_bookings 

的resourcefull路線只是減輕你鍵入的負擔CRUD操作的標準路由,但可以使用自定義路由進行擴展,例如,如果要爲PDF或CSV導出資源對象提供下載鏈接

然後,您可以在視圖文件中使用users_bookings_path指向採取行動

+0

謝謝。非常感謝幫助。 – Benamir

相關問題