祖先问题的多态注释 [英] Polymorphic Comments with Ancestry Problems

查看:98
本文介绍了祖先问题的多态注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试汇总两个Railscast: http://railscasts.com/episodes /262-trees-with-ancestry http://railscasts.com/episodes/我的应用上的154-polymorphic-association .

I am trying to roll together two Railscasts: http://railscasts.com/episodes/262-trees-with-ancestry and http://railscasts.com/episodes/154-polymorphic-association on my app.

我的模特:

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

在我的位置显示视图中,我有以下代码:

In my locations show view I have this code:

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

哪个输出正确.我有一个_comment.html.erb文件,它呈现每个注释等.还有一个_form.html.erb文件,该文件为新注释创建表单.

Which outputs properly. I have a _comment.html.erb file that renders each comment etc. and a _form.html.erb file that creates the form for a new comment.

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

The problem I have is that when I try <%= nested_comments @comments %> I get undefined method 'arrange'.

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

I did some Googling and the common solution to this was to add subtree before the arrange but that throws and undefined error also. I am guessing the polymorphic association is the problem here but I am at a loss as to how to fix it.

推荐答案

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

Dumb mistake... forgot to add the ancestry gem and required migration which I thought I had already done. The last place I checked was my model where I eventually discovered my error.

这篇关于祖先问题的多态注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆