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 之类的网址。此路径需要:id 参数。您可以通过多种方式调用此路线。其中之一是:
edit_post_path(@post)
如果你想创建一个自定义动作,(比如在帖子控制器下),你可以声明如下:
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