开发者

Exposing a Specific Action of a Nested Route

开发者 https://www.devze.com 2023-03-22 21:20 出处:网络
Using this as an example contexted: Post has_many comments Comment belongs_to post I have a route that looks l开发者_如何学Cike:

Using this as an example contexted: Post has_many comments Comment belongs_to post

I have a route that looks l开发者_如何学Cike:

resources :posts do
  resources :comments
end

How do i create a route that exposes comments#index?

An example use case would be... I want to list ALL comments in the system on a page. Essentially using the comments resource as if it's not nested when a user hits /comments

thank you!


Try this.

resources :posts do
  resources :comments, :except => :index
end
match 'comments' => 'comments#index', :as => :comments

That said, I usually look to avoid routes like this because I like a tidy RESTful routes file, but sometimes it can't be helped.

Second option:

resources :posts do
  resources :comments, :except => :index
  get :comments, :on => :collection
end

In the second option, you'd want to remove the index action from the comments controller and create a comments action in your posts controller.

0

精彩评论

暂无评论...
验证码 换一张
取 消