rails 4中关于匹配关键字的路由问题在rails 3中工作
在rails 3匹配关键字正在工作但在rails 4匹配关键字不适用于路由
如何在rails 4中定义这些路由
此代码段正在rails 3中运行
match 'admin', :to => 'access#menu' match 'show/:id', :to => 'public#show' match ':controller(/:action(/:id(.:format)))'
我需要轨道4的通用公式,如轨道3
match ':controller(/:action(/:id(.:format)))'
Rails 4删除了通用match
,您现在必须指定您希望它响应的动词。 通常,您将路线定义为:
get ':controller(/:action(/:id(.:format)))' => 'foo#matcher'
如果你想让它使用匹配来获得多个动词,你可以这样做:
match ':controller(/:action(/:id(.:format)))' => 'foo#matcher', via: [:get, :post]
文档中说的是什么:
通常,您应该使用get,post,put和delete方法来约束到特定动词的路由。 您可以将match方法与:via选项一起使用以匹配多个动词:
match 'photos', to: 'photos#show', via: [:get, :post]
您可以使用via :: all将所有动词与特定路线匹配:
match 'photos', to: 'photos#show', via: :all
(文档)
最好的方法是先找到佣金路线的输出
$ rake routes
你会得到这样的东西:
[rails path] [verb] [url format] [controller#action]
例如:
user_show GET /show/:id public#show
这个动作就是你需要看的东西。 你应该使用get
或post
而不是match
– 就像这样:
get 'show/:id', :to => 'public#show'
post ':controller(/:action(/:id(.:format)))'