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