I don’t know what I’m doing wrong. This is my route:
resources :stores do
get '/add_shoes' => 'stores#add_shoes', :as => :add_shoes
end
And my path should be: <%= link_to "Add Shoes", add_shoes_path %>
But it gives the error that the path does not exist. How do I use both of them?
If I recall correctly, because it’s nested in a
resourcesblock, it’s going to appendstoresto the end. So the correct route is add_shoes_stores_path. Sometimes it also adds an index to the end (not sure why), to give you add_shoes_stores_index_path.Since it’s a
getcall, you could always put it outside theresourcesblock.Additionally, the more Rails way to do it would be:
You’ve got a bunch of options here on how you want to handle it. But just a quick tip, you can always type
rake routesfrom the command line to get a list of all available routes and where they point.If you’re looking at doing it this way for your clarity of code, just remember this: Your routes should always mention where they’re pointing to. That would be both
add_shoesandstores. Having anadd_shoes_pathcould point to literally any controller, as it’s not really verbose. I would definitely stick to the Rails way of doing it – it will make more sense as you dive in deeper.Finally, another thought – If you’re adding shoes in the stores model, it would make sense for each store to have shoes. You should probably create a new model for Shoes and use RESTFUL routing.