如何在rails中定义没有模型名称的资源路由

时间:2013-12-27 16:12:27

标签: ruby-on-rails routes

使用resources :posts, only: :show { get :detail }我会得到这样的路线。

post_detail GET    /posts/:post_id/detail(.:format)                    posts#detail
post GET    /posts/:id(.:format)                                posts#show

我可以设置post_detail的路由:id而不是:post_id吗?

我知道我可以按get '/posts/:id/detail', to: 'posts#detail', as: 'post_detail'设置路线,但我想知道resources是否有办法。

1 个答案:

答案 0 :(得分:0)

你可以add member routes

resources :posts, only: :show do
  member do
    get 'detail'
  end
end

或没有阻止:

resources :posts, only: :show do
  get 'detail', on: :member
end

这会创建以下路线:

detail_post GET    /posts/:id/detail(.:format)    posts#detail
       post GET    /posts/:id(.:format)           posts#show