2012-07-09 54 views
0

設置多態時遇到了很多麻煩。我有評論工作正常,但鏈接應該工作完全一樣的鏈接#索引中有NameError。如果我嘗試forum_posts/1 /評論一切都很好,但不是forum_posts/1/links。多態未初始化常量

我是繼railscast教程http://railscasts.com/episodes/154-polymorphic-association?view=asciicast

欣賞的幫助。

錯誤

NameError在鏈接#索引

未初始化的常數ForumPost ::鏈接 提取的源(圍繞線#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 %> 

回答

0

Links模型應放在app/models/link.rb,並應稱爲Link,而不是Links

+0

非常感謝瑞恩。本週末的大部分時間都在嘗試對此進行分類。 :) – otissv 2012-07-09 11:16:25