How do I write a route in Rails 5 so that when a user visits
http:www.mysite.com/volunteer_events/add_shift
it fires the add_shift method inside the volunteer_events controller
I have spent hours staring at the routing page here...https://guides.rubyonrails.org/v5.0.7/routing.html ...and it's just turning into a giant wall of text.
This is what I currently have...
get '/volunteer_events/add_shift', to: 'volunteer_events#add_shift'
But I'm getting an error
Couldn't find VolunteerEvent with 'id'=add_shift
I know this is simple...I'm just very tired and I don't remember.
CodePudding user response:
How does the rest of your route.rb
file look?
Routes are interpreted from top to bottom with routes defined higher having a higher precedence. My assumption is that you have something like this in the routes
resources :volunteer_events
get '/volunteer_events/add_shift', to: 'volunteer_events#add_shift'
which would translate to something like this
get '/volunteer_events/:id', to: 'volunteer_events#show'
get '/volunteer_events/add_shift', to: 'volunteer_events#add_shift'
Now your first route has always precedence over the second one and add_shift
will always be interpreted as the id for the show
route (note the :id
in the show routes which is a parameter). Just flip the two so /volunteer_events/add_shift
will be matched before your show route and you should be good.