Marat_Galiev
Marat_Galiev

Reputation: 1293

Rails 3 form_for action with params

<%  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/>

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.

Upvotes: 0

Views: 6596

Answers (2)

tommasop
tommasop

Reputation: 18765

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])

Upvotes: 1

Nikita Barsukov
Nikita Barsukov

Reputation: 2984

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.

Upvotes: 0

Related Questions