Reputation: 1117
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%>
Upvotes: 1
Views: 240
Reputation: 27374
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 the do
block:
resources :appointments do
resources :bookings do
get 'user_bookings', :on => :collection
end
end
This will add an additional route for a GET
request to `/appointments/:appointment_id/bookings/user_bookings' and route it to 'bookings#user_bookings'.
Upvotes: 1
Reputation: 2124
have you tried to define a custom route which points to the defined action in your controller, such as:
match 'appointments/:id/users_bookings' => 'bookings#users_bookings', :as => :users_bookings
the resourcefull routes just relieve you from the burden of typing the standard routes for the CRUD-actions but can be extended with custom routes, in example if you want to offer a download link for a PDF or CSV export of a resource object
you can then use users_bookings_path in your view file to point to the action
Upvotes: 0
Reputation: 83
I would change it to be the index action of the bookings controller. That way it would match '/appointments/1/bookings'.
However if there's a reason you can't do that, as it's not one of the standard routes you'll need to specify it in the routes.rb file. Something like:
match '/appointments/:id/bookings/users_bookings' => 'bookings#users_bookings'
Upvotes: 0