Forum: Ruby on Rails Is it an ok practice to define a resource in your routes file multiple times? 3+ times?

4da8f502012ef8003af59bf5f4ddad0c?d=identicon&s=25 Chris P. (chris_p)
on 2014-06-09 20:59
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?
F50d3b02eee623a2172b58c09fe31c2c?d=identicon&s=25 mike2r (Guest)
on 2014-06-10 14:55
(Received via mailing list)
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]
4da8f502012ef8003af59bf5f4ddad0c?d=identicon&s=25 Chris P. (chris_p)
on 2014-06-10 19:55
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?
F50d3b02eee623a2172b58c09fe31c2c?d=identicon&s=25 mike2r (Guest)
on 2014-06-11 15:40
(Received via mailing list)
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
4da8f502012ef8003af59bf5f4ddad0c?d=identicon&s=25 Chris P. (chris_p)
on 2014-06-11 17:04
Thanks!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.