In rails 3 match keyword is working but in rails 4 match keyword is not working for routes
how can i define these routes in rails 4
this code segment is working in rails 3
match 'admin', :to => 'access#menu'
match 'show/:id', :to => 'public#show'
match ':controller(/:action(/:id(.:format)))'
i need generic formula for rails 4 like in rails 3
match ':controller(/:action(/:id(.:format)))'
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.
TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.
Rails 4 removed generic match
, you now have to specify which verb you want it to respond to. Typically you'd define your routes as:
get ':controller(/:action(/:id(.:format)))' => 'foo#matcher'
If you want it to use match to get multiple verbs, you can do it like this:
match ':controller(/:action(/:id(.:format)))' => 'foo#matcher', via: [:get, :post]
What is said in documentation:
In general, you should use the get, post, put and delete methods to constrain a route to a particular verb. You can use the match method with the :via option to match multiple verbs at once:
match 'photos', to: 'photos#show', via: [:get, :post]
You can match all verbs to a particular route using via: :all:
match 'photos', to: 'photos#show', via: :all
(documentation)
the best way to do this is to first find the output of rake routes
$ rake routes
you'll get something like this:
[rails path] [verb] [url format] [controller#action]
so for example:
user_show GET /show/:id public#show
the action is what you need to look at. You should be using get
or post
rather than match
- like this:
get 'show/:id', :to => 'public#show'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With