Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:28:30+00:00 2026-05-16T18:28:30+00:00

In config/routes.rb: resources :posts do resources :comments end resources :pictures do resources :comments end

  • 0

In config/routes.rb:

resources :posts do
  resources :comments
end

resources :pictures do
  resources :comments
end

I would like to allow for more things to be commented on as well.

I’m currently using mongoid (mongomapper isn’t as compatible with Rails 3 yet as I would like), and comments are an embedded resource (mongoid can’t yet handle polymorphic relational resources), which means that I do need the parent resource in order to find the comment.

Are there any elegant ways to handle some of the following problems:

In my controller, I need to find the parent before finding the comment:

if params[:post_id]
  parent = Post.find(params[:post_id]
else if params[:picture_id]
  parent = Picture.find(params[:picture_id]
end

which is going to get messy if I start adding more things to be commentable.

Also url_for([comment.parent, comment]) doesn’t work, so I’m going to have to define something in my Comment model, but I think I’m also going to need to define an index route in the Comment model as well as potentially an edit and new route definition.

There might be more issues that I have to deal with as I get further.

I can’t imagine I’m the first person to try and solve this problem, are there any solutions out there to make this more manageable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:28:30+00:00Added an answer on May 16, 2026 at 6:28 pm

    I had to do something similar in an app of mine. I took what I came up with and changed it around a bit, but I haven’t tested it, so use with care. It’s not pretty, but it’s better than anything else I was able to think of.

    In routes.rb:

    resources :posts, :pictures
    
    controller :comments do
      get '*path/edit' => :edit, :as => :edit_comment
      get '*path'      => :show, :as => :comment
      # etc. The order of these is important. If #show came first, it would direct /edit to #show and simply tack on '/edit' to the path param.
    end
    

    In comment.rb:

    embedded_in :commentable, :inverse_of => :comments
    
    def to_param
      [commentable.class.to_s.downcase.pluralize, commentable.id, 'comments', id].join '/'
    end
    

    In a before filter in comments_controller.rb:

    parent_type, parent_id, scrap, id = params[:path].split '/'
    
    # Security: Make sure people can't just pass in whatever models they feel like
    raise "Uh-oh!" unless %w(posts pictures).include? parent_type
    
    @parent = parent_type.singularize.capitalize.constantize.find(parent_id)
    @comment = @parent.comments.find(id)
    

    Ok, ugliness over. Now you can add comments to whatever models you want, and simply do:

    edit_comment_path @comment
    url_for @comment
    redirect_to @comment
    

    And so on.

    Edit: I didn’t implement any other paths in my own app, because all I needed was edit and update, but I’d imagine they’d look something like:

    controller :comments do
      get    '*path/edit' => :edit, :as => :edit_comment
      get    '*path'      => :show, :as => :comment
      put    '*path'      => :update
      delete '*path'      => :destroy
    end
    

    The other actions will be trickier. You’ll probably need to do something like:

      get  ':parent_type/:parent_id/comments'     => :index, :as => :comments
      post ':parent_type/:parent_id/comments'     => :create
      get  ':parent_type/:parent_id/comments/new' => :new,   :as => :new_comment
    

    You’d then access the parent model in the controller using params[:parent_type] and params[:parent_id]. You’d also need to pass the proper parameters to the url helpers:

    comments_path('pictures', 7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.