views:

308

answers:

2

how do i pass a polymorphic object to another controller?

for example redirecting from messages/1/

to requests/new?object_type=message&object_id=1

or, second example, from files/154/

to requests/new?object_type=file&object_id=154

is

redirect_to new_request_path(:object_type => params[:controller].classify, :object_id => params[:id])

right?

Request model has

  belongs_to      :object , :polymorphic => true
+1  A: 

You nest your routes, for example:

messages/1/requests/new
files/154/requests/new

redirect_to new_comments_request_path(Comment.find(1))

routes guide

amikazmi
thanks for information on nested routes. in my case, all polymorphic objects, like Message,File,Comment etc get redirected in the same method. is there any way to use something like "new_request_path(@polymorphicobject)" instead of "new_comments_request_path", "new_messages_request_path" etc separately?
Pavel K.
you can use http://github.com/josevalim/inherited_resources, if you use it there is a url helper "new_resource_url"- I think it's what you look for.
amikazmi
A: 

i accepted amikazmi's answer because information about routes nesting led me to polymorphic_url/polymorphic_path helper, which were response to my question

Pavel K.