views:

317

answers:

3

How do we pass parameters in redirect_to in rails? I know we can pass id using this:

redirect_to :action => action_name,:id => 3

If I want to pass additional parameters like some form data how to achieve it?

+3  A: 

Just append them to the options:

redirect_to :controller => 'thing', :action => 'edit', :id => 3, :something => 'else'

Would yield /thing/3/edit?something=else

Michael Sepcot
but I don't want it to be visible to user....
markiv
You can't redirect with a POST. From the HTTP 1.1 docs under the 3xx definitions: "The action required MAY be carried out by the user agent without interaction with the user if and only if the method used in the second request is GET or HEAD."Expand on what you're really trying to accomplish and we can probably push you in the correct direction.
jdl
Hi Thank a lot for your response.I am a newbie to web development.I am trying to know different ways to invoke an action.Your response has clarified lots of my doubts.Thanks again :))
markiv
A: 

Thanks Michael!

It worked with a charm :-)

love-n-peace,

@shaan

shaan
A: 

If you are using RESTful resources you can do the following:

redirect_to action_name_resource_path(resource_object, {:param_1 => 'value_1', :param_2 => 'value_2'})

or
#You can also use the object_id instead of the object
redirect_to action_name_resource_path(resource_object_id, {:param_1 => 'value_1', :param_2 => 'value_2'})

or
#if its a collection action like index, you can omit the id as follows
redirect_to action_name_resource_path({:param_1 => 'value_1', :param_2 => 'value_2'})

#An example with nested resource is as follows:
redirect_to edit_user_project_path(@user, @project, {:param_1 => 'value_1', :param_2 => 'value_2'})
Sohan