1

私は 2 つの Railscast をまとめようとしています: 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、アレンジの前に追加することでしたが、それはスローされ、未定義のエラーも発生します。ここではポリモーフィックな関連付けが問題であると推測していますが、それを修正する方法について途方に暮れています。

4

1 に答える 1

0

ばかげた間違い...先祖の宝石を追加するのを忘れており、すでに行っていると思っていた移行が必要でした。最後にチェックした場所は、最終的にエラーを発見したモデルでした。

于 2012-05-07T03:20:37.547 に答える