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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:58:55+00:00 2026-06-07T05:58:55+00:00

having a lot of trouble setting up polymorphic. I have Comments which works fine,

  • 0

having a lot of trouble setting up polymorphic. I have Comments which works fine, but the Links which should work exactly the same has NameError in Links#index. If I try to forum_posts/1/comments all is good but not forum_posts/1/links.

I was following the railscast tutorial http://railscasts.com/episodes/154-polymorphic-association?view=asciicast

Appreciate the help.

Error

NameError in Links#index

uninitialized constant ForumPost::Link
Extracted source (around line #4):

1: <h1>Links</h1>
2: 
3: <ul id="links">
4:   <% @links.each do |link| %>
5:     <li><%= link.display_name %></li>
6:   <% end %>
7: </ul>

routes

resources :forum_posts do
  resources :comments
  resources :links
end

Models

*models/forum_posts.rb*

class ForumPost < ActiveRecord::Base
  attr_accessible :content, :display_name, :section, :user_id
  has_many :comments, :as => :commentable
  has_many :links, :as => :linkable
end

models/comments.rb

class Comment < ActiveRecord::Base
  attr_accessible :commentable_id, :commentable_type, :content, :user_id
  belongs_to :commentable, :polymorphic => true
end

models/comments.rb

class Links < ActiveRecord::Base
  attr_accessible :description, :display_name, :inamge, :linkable_id, :linkable_type,    :user_id
  belongs_to :linkable, :polymorphic => true
end

Controllers

*controllers/comments_controller.rb*

class CommentsController < ApplicationController

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

  def index
    @commentable = find_commentable
    @comments = @commentable.comments
  end

  def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment])
    if @comment.save
      flash[:notice] = "Successfully saved comment."
      redirect_to :id => nil
    else
      render :action => 'new'
    end
  end

end

*controller/links_controller.rb*

class LinksController < ApplicationController

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

  def index
    @linkable = find_linkable
    @links = @linkable.links
 end

 def create
   @linkable = find_linkable
   @link = @linkable.links.build(params[:link])
   if @link.save
     flash[:notice] = "Successfully saved link."
     redirect_to :id => nil
   else
     render :action => 'new'
   end
  end

end

Views

views/comments/index.html.erb

<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| %>
  <%= form.label :content %><br/>
  <%= form.text_area :content, :rows => 5 %><br/>
  <%= form.submit "Add comment" %>
<% end %>

views/links/index.html.erb

<h1>Links</h1>

<ul id="links">
  <% @links.each do |link| %>
    <li><%= link.display_name %></li>
  <% end %>
</ul>

<h2>New Link</h2>
<%= form_for [@linkable, Link.new()] do |form| %>
  <%= form.label :display_name %><br/>
  <%= form.text_area :display_name %><br/>
  <%= form.submit "Add link" %>
<% 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-07T05:58:56+00:00Added an answer on June 7, 2026 at 5:58 am

    Your Links model should be placed at app/models/link.rb and should be called Link, not Links.

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

Sidebar

Related Questions

Having a lot of trouble getting texture maps to work in openGL ES (iphone).
I'm having a lot of trouble getting my priority queue to recognize which parameter
I'm having a lot of trouble with my background worker. I have a background
I'm having a lot of trouble writing this regular expression: (?<=\s+|^\s*|\(\s*|\.)(?:item|item1|item2)(?=\s+|\s*$|\s*\)|\.) It works very
I have been having a lot of trouble getting cocotron to create a simple
I'm having a lot of trouble doing something very simple. I have a viewController
I am having a lot of trouble using .dbf files. I have an application
I'm having a lot of trouble setting a flip transition between storyboards. The app
I am having a lot of trouble understanding how the Android context menu works.
I'm having a lot of trouble trying to define a mock for a rails

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.