2016-05-20 25 views
1

我是Ruby on Rails的初学者。我有一个users_controller.rb有大约action大约100多。所以很难在路由器中设置。我想自动设置操作。如何在CakePHP中像ruby一样自动获取页面URL?

例子:

def abc 
    render text: 'Hello World!' 
end 
... 
... 
def xyz 
    render text: 'Bla Bla Bla' 
end 

我希望我的网址应该是开放的这种类型的

http://localhost:3000/users/abc 
http://localhost:3000/users/xyz 
http://localhost:3000/users/some-action.. 

无需设置路由器任何多行URL路径。

下面我写了路由器。卜代码不工作:

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

请帮我

+0

你需要这种类型的应用程序中的所有控制器的东西?或一个特定的控制器 – Thorin

+0

我认为你不应该使用这种类型的东西http://homakov.blogspot.in/2012/04/whitelist-your-routes-match-is-evil.html – Thorin

+0

什么是'dnd'? ...... – sawa

回答

3

使用你的代码,我得到的错误是:

You should not use the `match` method in your router without specifying an HTTP method. (ArgumentError) 
If you want to expose your action to both GET and POST, add `via: [:get, :post]` option. 

Rails 4.2.5.2ruby 2.3.0以下工作:

match ':controller(/:action(/:id(.:format)))', via: [:get, :post] 

请注意, via: [:get, :post]

+0

工作正常。以前我在路由器中设置了其他路径。在'match'之后是否有影响:controller(/:action(/:id(。:format)))',通过:[:get,:post]'? – Chinmay235

+0

Rails路由的工作原理如下:你有一个'config \ routes.rb'文件。 Rails将循环遍历每行,从上到下。如果当前网址与当前线路的路线相匹配,那么Rails会停止并使用该路线,否则它会转到下一行。基于这一点,我想你明白,如果你把'match':controller(/:action(/:id(。:format)))',通过:[:get,:post]它将(几乎)始终与路线相匹配,因此下面的所有行都是无用的。结论是:把这行放在你的'routes.rb'的底部。更多信息:http://guides.rubyonrails.org/routing.html – Pholochtairze

+0

是工作正常我的其他网址。谢谢亲爱的:) – Chinmay235