[英]rails rake routes where are they from
当你输入
rake routes
一堆路线出来了,但他们在哪里定义???
我知道有些是默认的,其他的怎么样?
例如,这是一个来自控制器的脚本,我试图从do_something中取消's',但是不能使它工作....它们是否也定义在其他地方? 此外,他们什么时候采取参数,什么时候不参加,我怎么知道它? 谢谢!
def hello
redirect_to do_things_shop_path(shop)
end
def do_things
end
Rails路由配置保存在config/routes.rb
文件中。
参数取决于许多事情。 rake routes
将显示带路线的参数。 成员行动将采取参数。
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
edit_post GET /posts/:id/edit(.:format) posts#edit
在最后一行,你会像posts/:id/edit
一样url。 此路径需要:id
参数。 您可以通过多种方式呼叫此路线。 其中一个是:
edit_post_path(@post)
如果要创建自定义操作(例如在posts控制器下),可以将其声明为:
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
第一个需要ID而第二个不需要。 相应地打电话:
do_things_with_id_path(@post)
do_things_without_id()
对于资源,您可以使用成员和集合操作轻松创建这些资源。 成员操作需要id,而集合操作则不需要。
resources :posts do
member { get 'do_thing' }
collection { get do_things' }
end
希望你明白了。
顺便说一下,如果你想清楚地理解这些,你必须阅读以下指南。 http://guides.rubyonrails.org/routing.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.