Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

route issue in rails 4 about match keyword working in rails 3

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)))'
like image 983
Khan Muhammad Avatar asked Nov 01 '13 07:11

Khan Muhammad


People also ask

What is match in Rails routes?

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.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

How do I see routes in Rails?

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.


3 Answers

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]
like image 172
Jonathan Bender Avatar answered Oct 13 '22 03:10

Jonathan Bender


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)

like image 40
Ivan Shamatov Avatar answered Oct 13 '22 04:10

Ivan Shamatov


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'
like image 35
dax Avatar answered Oct 13 '22 05:10

dax