1

I'm working on a simple reservation application. Here are my routes

resources :users do
  get 'reservations', on: :member
end

resources :listings do
  resources :reservations
end

When I try to make a reservation, action reservations#new takes me to reservations_path . Of course I'm getting error as this path doesn't exist. I'd like action new to take me to listing_reservations_path instead. I was hopping it will be done automatically since resources :reservations is in nested resources. I read about routes and tried many things but can't find any working way of doing it. Is it possible?

Natalia
  • 11
  • 1
  • first check routes.. through rake routes.. – Vishal Jul 23 '16 at 11:30
  • what do you mean by action `reservations#new` takes you to `reservations_path`? Do you mean to say that form helper when used with controller and action as `reservation` and `new` sets the path to `reservations_path`? – aBadAssCowboy Jul 23 '16 at 11:36

1 Answers1

1

You seem to be unclear on the nature of routes. The action reservations#new exists independently from any route. A route is just a way to map a URL path to a controller and action. If you are trying to do something like:

redirect_to controller: :resources, action: :new

You will have trouble, as all of your routes require some context. Instead, you need to provide whatever the URL helper you're using with a context:

redirect_to listing_reservations_path(@listing)

link_to "New Reservation", new_listing_reservation_path(@listing)    

link_to "Reservation", [@listing, @reservation]
Sam Coles
  • 4,003
  • 24
  • 19