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 の create アクションにコメント可能な型を渡すにはどうすればよいですか? ありがとう。

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

コメント可能なモデルがある場合は、コントローラーの create メソッドにオブジェクトを明示的に渡す必要はありません。

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

Commentable の大文字 C に注意してください。

于 2011-03-03T09:29:01.893 に答える