Is it an ok practice to define a resource in your routes file multiple times? 3+ times?

Noobie here. Let’s say you have a Post, Comment, and User models. So
Post has_many comments, and User has_many comments.

If it good practice to do the following…

resources :posts do
  resources :comments
end

resources :users do
  resources :comments
end

etc?

I have a model in my schema that I believe I will likely have 3
different route resources for.

Or is better to just have the one, such as our first snippet of code
above, and then have a param that modifies what is returned, in this
example, passing in the user_id to just see comments for that user?

IMO, it can be done either way. If you use routes, it would be a little
cleaner when you are using the same resource more than once to make a
concern:

concern :commentable do
resources :comments
end

resources :posts, concerns: [:commentable]
resources :users, concerns: [:commentable]

mike2r wrote in post #1149390:

IMO, it can be done either way. If you use routes, it would be a little
cleaner when you are using the same resource more than once to make a
concern:

concern :commentable do
resources :comments
end

resources :posts, concerns: [:commentable]
resources :users, concerns: [:commentable]

When doing this, does this mean both of these resources would access the
same controller? How can I have them access different controllers, and
what would a proper naming convention be for each controller in your
example?

On Tuesday, June 10, 2014 1:56:34 PM UTC-4, Ruby-Forum.com User wrote:

resources :posts, concerns: [:commentable]
resources :users, concerns: [:commentable]

When doing this, does this mean both of these resources would access the
same controller? How can I have them access different controllers, and
what would a proper naming convention be for each controller in your
example?


Posted via http://www.ruby-forum.com/.

If you’re referring to the comments resource, yes, both would post to
the
same controller in your format as well as the one I suggested. If you
want
them to point to different controllers, you will need to go back to your
original format and specify a controller, such as:

resources posts do
resources comments, controller: “postcomment”
end

Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs