My model summary: A user has many appointments. An appointment has many bookings. A Booking belongs to an appointment.
I’m trying to link_to a specific view (called “users_bookings”) that lists all the bookings for a specific appointment. Here is what I have tried:
<% current_user.appointments.each do |appointment|%>
<%= link_to "view all bookings", users_bookings_appointment_booking_path(appointment)%>
<%end%>
This is the error I get:
undefined method `users_bookings_appointment_bookings'
Additional Info:
Routes:
resources :appointments do
resources :bookings do
get 'users_bookings', :on => :collection
end
end
Bookings Controller create Action:
def create
@appointment = Appointment.find(params[:booking][:appointment_id])
@booking = @appointment.bookings.new(params[:booking])
Bookings controller Users_bookings Action:
def users_bookings
@appointment = Appointment.find(params[:booking][:appointment_id])
@bookings = @appointment.bookings.all
end
Users_bookings view:
<% @bookings.each do |booking| %>
<td><%= booking.appointment_date%></td>
<td><%= booking.start_time %></td>
<td><%= booking.end_time %></td>
<%end%>
You shouldn’t use
match(as others have suggested) unless you really want to match all HTTP requests (GET,POST, etc.) for that URL. Instead, just add a route to the resource in thedoblock:This will add an additional route for a
GETrequest to `/appointments/:appointment_id/bookings/user_bookings’ and route it to ‘bookings#user_bookings’.