在Rails中创建多态关联的表单

时间:2010-09-20 03:45:51

标签: ruby-on-rails polymorphic-associations

我有几个课程,每个课程都有评论:

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

(其中“text”是Comment表中的一个字段) 但这也不起作用。

我实际上并不确定如何将评论与电影相关联。例如,

c = Comment.create(:text => "This is a comment.", :commentable_id => 1, :commentable_type => "movie") 

似乎没有创建与ID为1的影片关联的注释。(Movie.find(1).comments返回一个空数组。)

2 个答案:

答案 0 :(得分:7)

由于您已在模型中创建了多态关联,因此您无需再在视图中担心。您只需在评论控制器中执行此操作。

@movie = Movie.find(id) # Find the movie with which you want to associate the comment
@comment = @movie.comments.create(:text => "This is a comment") # you can also use build
# instead of create like @comment = @movie.comments.create(:text => "This is a comment")
# and then @comment.save
# The above line will build your new comment through the movie which you will be having in
# @movie.
# Also this line will automatically save fill the commentable_id as the id of movie and 
# the commentable_type as Movie.

答案 1 :(得分:3)

你必须更具描述性而不是“......但这也不起作用”,但总体思路是:

@movie.comments.create( :text => params[:movie][:comment][:text] )

更典型的是:

@movie.comments.create( params[:comment] ) # or params[:movie][:comment]

重要的是,您首先找到@movie并通过它创建关联的对象。这样您就不必担心Commentable或类型或任何东西。