0
<%  form_for [commentable, Comment.new], :action => 'create', :remote => false do |f|%>
<%=f.hidden_field :commentable_id, :value=> commentable.id %><br/>
<%=f.hidden_field :parent_id, :value=>1 %><br/>

和一个控制器:

def create(commentable)
@commentable = commentable.find(params[:comment][:commentable_id])

如何将可评论类型传递给我的 for_for 中的创建操作?谢谢。

4

2 回答 2

1

你需要使用

commentable.class

沿着你已经做过的事情,你可以使用一个隐藏字段:

<%=f.hidden_field :commentable_type, :value=> commentable.class %><br/>

然后在控制器中:

@commentable = Object.const_get(params[:comment][:commentable_type]).find(params[:comment][:commentable_id])
于 2011-03-03T08:56:15.013 回答
0

如果您有可评论的模型,则无需将对象显式传递给您在控制器中创建方法:

def create
  @commentable = Commentable.find(params[:comment][:commentable_id])
  #more code
end

注意 Commentable 中的大写 C。

于 2011-03-03T09:29:01.893 回答