Ruby-on-rails rails rake 路线他们来自哪里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13391143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails rake routes where are they from
提问by runcode
When you type
当你输入
rake routes
a bunch of routes come out, but where are they defined???
一堆路由出来了,但它们在哪里定义???
I know some are default, and how about the others?
我知道有些是默认的,其他的呢?
For example, this is a script from a controller, I tried to take off the 's' from do_something, but can't make it work.... are they defined somewhere else too? Also, when do they take parameters and when not, how I know it ? Thanks!
例如,这是一个来自控制器的脚本,我试图从 do_something 中去掉 's',但无法使其工作......它们是否也在其他地方定义?另外,他们什么时候接受参数,什么时候不接受,我怎么知道?谢谢!
def hello
redirect_to do_things_shop_path(shop)
end
def do_things
end
回答by HungryCoder
Rails routing configurations are kept in config/routes.rbfile.
Rails 路由配置保存在config/routes.rb文件中。
Taking parameters depends on many things. rake routeswill show with routes take parameters. Member actions will take parameters.
取参数取决于很多事情。rake routes将显示路由带参数。成员操作将采用参数。
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
edit_post GET /posts/:id/edit(.:format) posts#edit
In the last line, you will url like posts/:id/edit. This path requires :idparameter. You can call this route many ways. One of them is like:
在最后一行,你会像posts/:id/edit. 此路径需要:id参数。您可以通过多种方式调用此路线。其中之一是这样的:
edit_post_path(@post)
If you want to create a custom action, (say under posts controller), you can declare it as follow:
如果你想创建一个自定义操作,(比如在帖子控制器下),你可以声明如下:
match `/posts/:id/things_with_id`, :to => 'posts#do_things_with_id', :as => 'do_things_with_id
match `/posts/things_without_id`, :to => 'posts#do_things_without_id', :as => 'do_things_without_id
First one requires an ID while the second one does not. Call them accordingly:
第一个需要ID,而第二个不需要。相应地调用它们:
do_things_with_id_path(@post)
do_things_without_id()
For a resource, you can create these easily using member & collection action. Member action needs id while collection action does not.
对于资源,您可以使用成员和集合操作轻松创建这些资源。成员操作需要 id,而收集操作则不需要。
resources :posts do
member { get 'do_thing' }
collection { get do_things' }
end
hope you got it.
希望你明白了。
By the way, you must read the following guide if you want to understand these clearly. http://guides.rubyonrails.org/routing.html
顺便说一下,如果您想清楚地理解这些,您必须阅读以下指南。 http://guides.rubyonrails.org/routing.html

