1

我正在尝试将两个 Railscast 组合在一起:http ://railscasts.com/episodes/262-trees-with-ancestry和http://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在安排之前添加,但这也会引发和未定义的错误。我猜多态关联是这里的问题,但我不知道如何解决它。

4

1 回答 1

0

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

于 2012-05-07T03:20:37.547 回答