2012-04-29 73 views
1

我想在我的应用上将两个Railscasts合并到一起:http://railscasts.com/episodes/262-trees-with-ancestryhttp://railscasts.com/episodes/154-polymorphic-association祖先问题的多态性评论

我的模型:

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

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

我的控制器:

class LocationsController < ApplicationController 
     def show 
     @location = Location.find(params[:id]) 
     @comments = @location.comments.arrange(:order => :created_at) 

     respond_to do |format| 
      format.html # show.html.erb 
      format.json { render json: @location } 
     end 
     end 
end 

class CommentsController < InheritedResources::Base 

    def index 
    @commentable = find_commentable 
    @comments = @commentable.comments.where(:company_id => session[:company_id]) 
    end 

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

    private 

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

end 

在我的位置显示视图我有这样的代码:

<%= render @comments %> 
<%= render "comments/form" %> 

哪个正常输出。我有一个_comment.html.erb文件,用于呈现每个评论等,以及一个_form.html.erb文件,该文件为新评论创建表单。

我的问题是,当我尝试<%= nested_comments @comments %>我得到undefined method 'arrange'

我做了一些谷歌搜索和常见的解决方案是在排列之前添加subtree,但也引发和未定义的错误。我猜测多态关联是这里的问题,但我对如何解决这个问题不知所措。

回答

0

愚蠢的错误...忘了添加祖先的宝石和需要的迁移,我认为我已经做了。我检查的最后一个地方是我的模型,最终发现我的错误。