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 3612402
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:59:40+00:00 2026-05-18T21:59:40+00:00

I am having a bit of trouble getting these polymorphic associations to work completely.

  • 0

I am having a bit of trouble getting these polymorphic associations to work completely. I followed this tutorial http://www.railscasts.com/episodes/154-polymorphic-association, but that seems to only work if I am path /controller/ID#/comments when making a new post. If I try to render a partial comment form right on /controller/ID# I get this error when creating the comment:

undefined method `comments' for #<ActiveSupport::HashWithIndifferentAccess:0x10341b2d0>

I have three models:

class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true       
end


class Post < ActiveRecord::Base
    belongs_to :user
    has_many :comments, :as => :commentable
end

class Article < ActiveRecord::Base
    belongs_to :user
    has_many :comments, :as => :commentable
end

And this is my view for /articles/#{ID}

<%= @article.title %>
<%= @article.content %>
<%= @article.user.login %>
<%= link_to 'Edit Article', edit_article_path(@article) %>

<h2>Comments</h2>
<%= render "comments/comments" %>
<%= render "comments/comment" %>

And here is my comment partial:

<div class="post_comment">
<%= form_for [@commentable, Comment.new] do |f| %>
    <%= f.text_area :content %>
    <%= f.submit "Post" %>
<% end %>
</div>

Here is my create method in the comments controller:

def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment])
    @comment.user_id = current_user.id
    if @comment.save
        redirect_to :id => nil
    else    
        flash[:notice] = "something went wrong"
        redirect_to @commentable
    end 
end

def find_commentable
    params.each do |name, value|
        if name =~ /(.+)_id$/
            return $1.classify.constantize.find(value)
        end
    end
end

I think I understand what the issue is, but not sure how to correct the problem. This form is looking for @commentable, but if the path isn’t nested it cannot find @commentable (may be wrong).

Here are my routes:

devise_for :users do
        get "login", :to => "devise/sessions#new"
        get "register", :to => "devise/registrations#new"
    end

    resources :posts do
        resources :comments
        resources :tags
    end

    resources :articles do
        resources :comments
        resources :tags
    end

    resources :users do 
        resources :articles
        resources :comments
        resources :tags
        resources :posts
    end 

    resources :comments

    root :to => "home#index"

end
  • 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-18T21:59:41+00:00Added an answer on May 18, 2026 at 9:59 pm

    Your issue is that find_commentable is returning the params hash itself because the return statement never fired.

    I deduced this by the error message because params.class == ActiveSupport::HashWithIndiffrentAccess

    def find_commentable
        params.each do |name, value|
            if name =~ /(.+)_id$/
                return $1.classify.constantize.find(value)
            end
        end
    end
    

    I would add something like

    def find_commentable
        params.each do |name, value|
            if name =~ /(.+)_id$/
                return $1.classify.constantize.find(value)
            end
        end
        raise ActiveRecord:NoRecord.new("Couldn\'t find it captain!")
    end
    

    The above exception type I think is correct, if not see what Comment.find_by_id(-1) or something like that raises. Should be something along those lines.

    Edit

    The reason I suggest raising the exception is because ApplicationController should be catching those and handling them gracefully when people start poling around and go to example.com/posts/9001

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a bit of config trouble in Wordpress and wonder if anyone can help.
I'm exporting a grid with NPOI v1.2.3, and am having trouble getting cell formatting
I'm having a bit of trouble in Google App Engine ensuring that my data
I'm having a bit of trouble understanding the implementation of MVC in Ruby on
I've been looking at this for a while. I've found plenty of posts and
A few days ago i asked this question: Displaying images from Facebook photo albums
I am trying to set the default value for a form field in SharePoint
I'm new to NHibernate, and am trying to map a domain model that has
I am messing around with a tornado web app with which I need a
I am using Mongoid and have a project and a user model. in the

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.