多态未初始化常数

时间:2012-07-09 07:46:01

标签: ruby-on-rails

设置多态时遇到很多麻烦。我有一些工作正常的评论,但链接应该工作完全相同的链接#index有NameError。如果我尝试forum_posts / 1 /评论一切都很好,但不是forum_posts / 1 /链接。

我正在关注railscast教程http://railscasts.com/episodes/154-polymorphic-association?view=asciicast

感谢帮助。

错误

  

Links#index中的NameError

     

未初始化的常量ForumPost :: Link   提取的来源(第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>

路由

resources :forum_posts do
  resources :comments
  resources :links
end

模型

*模型/ 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

模型/ comments.rb

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

模型/ comments.rb

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

控制器

*控制器/ 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

*控制器/ 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

视图

视图/评论/ 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 %>

视图/链接/ 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 个答案:

答案 0 :(得分:0)

您的Links模型应放在app/models/link.rb,应该调用Link,而不是Links