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

  • Home
  • SEARCH
  • 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 7791429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:53:02+00:00 2026-06-01T21:53:02+00:00

I am working on my first polymorphic association relationship and I am having trouble

  • 0

I am working on my first polymorphic association relationship and I am having trouble refactoring my form_for create comments.

I tried going through the Polymorphic Association RailsCasts http://railscasts.com/episodes/154-polymorphic-association?view=asciicast, but it seems dated.

I have two questions:

  1. How do I rewrite my comment_form partial so that it would work for anything commentable? The way I have it right now, it would only work for traveldeals since (:commentable_id => @traveldeal.id).

  2. When I create the comment, the commentable_type is empty. What is commentable_type and do I need to pass it in the form?

Thanks!

user.rb

class User < ActiveRecord::Base
  has_many :comments, :dependent => :destroy
end

traveldeal.rb

class Traveldeal < ActiveRecord::Base
  has_many :comments, :as => :commentable, :dependent => :destroy
end

comment.rb

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

  validates :user_id, :presence => true
  validates :commentable_id, :presence => true
  validates :content, :presence => true
end

traveldeal_show.html.erb

<%= render 'shared/comment_form'  %>

_comment_form.html.erb

<%= form_for current_user.comments.build(:commentable_id => @traveldeal.id) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>

<div>
  <%= f.text_area :content %>
</div>

<%= f.hidden_field :user_id %>
<%= f.hidden_field :commentable_id %>

<div>
  <%= f.submit "Add Comment" %>
</div>
<% end %>

comments_controller.rb

class CommentsController < ApplicationController
  before_filter :authenticate, :only => [:create, :destroy]

  def create
    @comment = Comment.new(params[:comment])
    @comment.save
    redirect_to root_path
  end

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-06-01T21:53:05+00:00Added an answer on June 1, 2026 at 9:53 pm

    The only part that is dated in the Railscast are the routes.

    To answer your first question: create your form like it is done in the Railscast:

    <%= form_for [@commentable, Comment.new] do |f| %>
      <p>
        <%= f.label :content %><br />
        <%= f.text_area :content %>
      </p>
      <p><%= f.submit "Submit" %></p>
    <% end %>
    

    If you do it like this commentable_type will be set automatically. You need the type so you know to what model a comment belongs. Note that you have to set @commentable in the method where you use the comment form.

    E.g.

    class TraveldealsController < ApplicationController
      def show
        @traveldeal = @commentable = Traveldeal.find(params[:id])
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on my first Rails project, I used Ryan Bates' Nifty-Generators to create the
I'm working on my first PHP project, and it's going well. I've been able
I'm attempting to create a Product Engine for Apostrophe. I'm having trouble extending the
I am new to wpf and i am working on first application i have
For some weird reason, the codes below are first working, then website is redirecting
I working on my first ASP.net MVC project and and i got some problems
I'm working on my first object oriented bit of python and I have the
I'm working on my first 'real' F# assembly, and trying to do things right.
I'm working on a first symfony project, i am using the sfWidgetFormTextareaTinyMCE widget for
I'm working on my first Core Data project (on iPhone) and am really liking

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.