I made the same question on StackOverflow.
I'm trying to understand Polymorphic Associations in Rails 3.2, but I'm having a hard time find good resources about it. I am trying to follow a RailsCasts episode about it, but it's pretty outdated, so I can't get it right. Here's what I've done so far:
I made a test application that has 2 independent models:
Foo
and BarCode:class CreateFoos < ActiveRecord::Migration def change create_table :foos do |t| t.string :name t.text :content t.timestamps end end end
After that, I made the Comment model which will have a polymorphic association with Foo and Bar:Code:class CreateBars < ActiveRecord::Migration def change create_table :bars do |t| t.string :name t.text :content t.timestamps end end end
Code:class CreateComments < ActiveRecord::Migration def change create_table :comments do |t| t.text :content t.integer :commentable_id t.string :commentable_type t.timestamps end end endAfterwards, I made the association to the other models:Code:class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end
I think I got the associations right. As for the routing, the routes.rb shown at the RailsCasts episode was pretty difference from what I was used to. I think this is because I started Rails at v3.0. Here's what I did:Code:class Foo < ActiveRecord::Base has_many :comments, :as => :commentable end class Bar < ActiveRecord::Base has_many :comments, :as => :commentable end
I'm not sure if this is the right way to do it, but it works. Following the RailsCasts episode, I made the method for finding which model the comment is associated with:Code:AnotherExerciseApp::Application.routes.draw do resources :foos do resources :comments resources :bars do resources :comments end end end
Afterwards, I changed the index action of the CommentsController to use the newly constructed method:Code:def find_commentable params.each do |name, value| if name =~ /(.+)_id$/ return $1.classify.constantize.find(value) end end nil end
Here's where I went wrong: I have placed this code into the index page of the Comments view:Code:def index @commentable = find_commentable @comments = @commentable.comments end
For some reason, when I go to /foos/1/comments, the page is displaying, but the form isn't appearing. To be honest, I have no idea why that is the case. The only thing I noticed is that `form_for` is referencing to `@commentable`, which is the interface for the polymorphic association with the Comments model.Code:<h1>Comments</h1> <ul id="comments"> <% @comments.each do |comment| %> <li><%= comment.content %></li> <% end %> </ul> <h2>New Comment</h2> <% form_for [@commentable, Comment.new] do |form| %> <ol class="formList"> <li> <%= form.label :content %> <%= form.text_area :content, :rows => 5 %> </li> <li><%= submit_tag "Add comment" %></li> </ol> <% end %>
---------- Post added at 12:42 PM ---------- Previous post was at 12:21 PM ----------
As Katherine Pe has pointed out, the cause of the problem was a typo. I forgot to put an = on form_for. This is really embarrassing LOL. Kindly delete this shameful thread. Thanks!



Reply With Quote
