<% form_for [commentable, Comment.new], :action => 'create', :remote => false do |f开发者_JAVA百科|%>
<%=f.hidden_field :commentable_id, :value=> commentable.id %><br/>
<%=f.hidden_field :parent_id, :value=>1 %><br/>
And a controller:
def create(commentable)
@commentable = commentable.find(params[:comment][:commentable_id])
How I can pass commentable type to a create action in my for_for? Thanks.
You need to use
commentable.class
Along the lines of what you already did you can use a hidden field:
<%=f.hidden_field :commentable_type, :value=> commentable.class %><br/>
Then in controller:
@commentable = Object.const_get(params[:comment][:commentable_type]).find(params[:comment][:commentable_id])
You don't need to pass a object explicitly to you create method in controller, if you have commentable model:
def create
@commentable = Commentable.find(params[:comment][:commentable_id])
#more code
end
Note capital C in Commentable.
精彩评论