[英]Accessing an arbitrary action in rails
I am somewhat confused as to how to properly access an action in a rails controller that is NOT associated with one particular model. 对于如何正确访问与某个特定模型无关的rails控制器中的操作,我有些困惑。 The routing file, by default, seems to be routing the action name to "id".
默认情况下,路由文件似乎将操作名称路由为“ id”。 So if I type, say, /user/login, I end up with an error that says: "Couldn't find User with 'id'= login"
因此,如果我输入/ user / login,则会出现一个错误,提示:“找不到'id'= login的用户”
What is the proper way to access arbitrary action names in rails? 在rails中访问任意动作名称的正确方法是什么?
Make a route for it, obviously. 很明显,为此做一个路线。 The request goes this way:
请求是这样的:
resource
and resources
might not be obvious about what they do. resource
和resources
对于它们的作用可能并不明显。 But in fact, they are shorthands to corresponding collections of routes used quite often, like this is what resources
adds . 但是实际上,它们是经常使用的相应路由集合的简写,就像这就是
resources
补充 。 And they're not monoliths, they can be customised to fit your needs. 而且它们不是独石,可以根据您的需求进行定制。 Providing options is just a start, you can provide a block to define your custom action routes for this resource like so:
提供选项仅仅是一个开始,您可以提供一个块来定义此资源的自定义操作路线,如下所示:
resources :users do
get :login
end
This will add a /users/login
route that maps to UsersController#login
, following Rails' conventions. 这将遵循Rails的约定添加一个
/users/login
路由,该路由映射到UsersController#login
。
See this guide for more details and don't forget to run rake routes
to see what you have at the moment. 有关更多详细信息, 请参阅本指南 ,不要忘记运行
rake routes
以查看当前所拥有的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.