1
我正在使用多態關聯。多態關聯錯誤
我有2頁型號的文章和事件,必須使用多態關聯has_many
與評論模型關聯
內部意見控制器:
def index
@commentable = find_commentable
@comments = @commentable.comments
end
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
裏面的文章和事件模型,我寫道:
has_many :comments, :as => :commentable,:dependent => :destroy
內部評論模型:
belongs_to :commentable, :polymorphic => true
內部路線:
resources :articles do
resources :comments
end
我的問題是
1)當我進入http://localhost:3003/articles/8/comments它提供了以下錯誤:
undefined method `comments' for nil:NilClass
2)我也不想使用嵌套路線,我想用簡單的路線。我怎樣才能達到這個代碼所做的同樣的事情?
有ID爲8的文章....我想find_commentable功能不正確commentable返回......但我有1個更多的疑問,聞一點擊鏈接評論它去索引操作,然後find_commentable函數,當這個函數被執行時會在params和name內部發生,在這一行的值params.each do | name,value |在那個函數裏面 – Nikita 2012-03-02 06:18:35