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

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.

2
  • I think you need to reshape your question if you want further assistence, can you share more about your code? Commented Mar 3, 2011 at 9:26
  • in your controller, try @commentable = Commentable.create(params[:comment][:commentable_id]) Commented Apr 3, 2012 at 20:58

2 Answers 2

1

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])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but how I can pass my commentable object to create action?
Why pass the entire object? You can just pass an id and fetch the object later.
0

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.

1 Comment

Sure, but I use acts_as_commentable_with_threading, and Commentable class doesn't exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.