9

在 rails 3 match 关键字有效,但在 rails 4 match 关键字不适用于路由

我如何在 Rails 4 中定义这些路线

此代码段在 rails 3 中工作

match 'admin', :to => 'access#menu'

match 'show/:id', :to => 'public#show'

match ':controller(/:action(/:id(.:format)))'

我需要 Rails 4 的通用公式,就像在 Rails 3 中一样

 match ':controller(/:action(/:id(.:format)))'
4

4 回答 4

13

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]
于 2013-11-01T08:04:16.177 回答
6

文档中所说的:

通常,您应该使用 get、post、put 和 delete 方法来约束到特定动词的路由。您可以使用带有 :via 选项的 match 方法一次匹配多个动词:

match 'photos', to: 'photos#show', via: [:get, :post] 

您可以使用 via: :all: 将所有动词匹配到特定路线

match 'photos', to: 'photos#show', via: :all

(文档)

于 2013-11-01T08:11:03.590 回答
2

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'
于 2013-11-01T08:03:59.953 回答
0
post ':controller(/:action(/:id(.:format)))'
于 2014-10-17T18:25:23.893 回答