【问题标题】:Polymorphic Comments with Ancestry Problems具有祖先问题的多态注释
【发布时间】:2012-04-29 05:19:24
【问题描述】:

我正在尝试在我的应用程序上同时滚动两个 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 文件,用于为新评论创建表单。

我遇到的问题是,当我尝试&lt;%= nested_comments @comments %&gt; 时,我得到undefined method 'arrange'。

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

【问题讨论】:

    标签: ruby-on-rails-3 polymorphic-associations ancestry


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      相关资源
      最近更新 更多