我有几个班级,每个班级都有评论:
class Movie < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Actor < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
如何为新的电影评论创建表单?我添加了
resources :movies do
resources :comments
end
到我的routes.rb,并尝试了new_movie_comment_path(@movie),但这给了我一个包含commentable_id和commentable_type的表单[我想自动填充,而不是由用户直接输入]。我也尝试自己创建表单:
form_for [@movie, Comment.new] do |f|
f.text_field :text
f.submit
end
(其中“文本”是评论表中的一个字段)但这也不起作用。
我实际上根本不确定如何将评论与电影相关联。例如,
c = Comment.create(:text => "This is a comment.", :commentable_id => 1, :commentable_type => "movie")
似乎没有创建与 id 为 1 的电影关联的评论。(Movie.find(1).comments 返回一个空数组。)