Reputation: 2351
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?
EDIT:
For Ruby 2 syntax you have to update the snippet above to:
redirect_to action: action_name, id: 3
Upvotes: 223
Views: 268393
Reputation: 1707
As a polymorphic route...
redirect_to [@mything, {key: 'value'}]
or
redirect_to [:mythings, {key: 'value'}]
etc.
Upvotes: 0
Reputation: 1133
As of Rails 6, you can simply call redirect_to
followed by the path you wish to redirect to such as home_path
, and then pass is a hash of key-value pairs.
example:
redirect_to home_path(name: 'Jason', needs: 'help with rails', help: true)
After this, you will be able to retrieve these values from the params hash.
ex
params[:name]
to retrieve the string 'Jason'
Upvotes: 7
Reputation: 2537
Route your path, and take the params
, and return:
redirect_to controller: "client", action: "get_name", params: request.query_parameters and return
Upvotes: 7
Reputation: 41954
If you are looking for a way to pass additional URL parameters (not controller, action, id, etc), here's a robust method for doing so:
object_path(@object, params: request.query_parameters)
That will pass along utm parameters or any other additional params you don't want to lose.
Upvotes: 10
Reputation: 3805
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')
Upvotes: 93
Reputation: 479
You can pass arbitrary objects to the template with the flash parameter.
redirect_to :back, flash: {new_solution_errors: solution.errors}
And then access them in the template via the hash.
<% flash[:new_solution_errors].each do |err| %>
Upvotes: 39
Reputation: 11395
Just append them to the options:
redirect_to controller: 'thing', action: 'edit', id: 3, something: 'else'
Would yield /thing/3/edit?something=else
Upvotes: 189
Reputation: 179
routes.rb
match 'controller_name/action_name' => 'controller_name#action_name', via: [:get, :post], :as => :abc
Any controller you want to redirect with parameters are given below:
redirect_to abc_path(@abc, id: @id), :notice => "message fine"
Upvotes: 6
Reputation: 2932
redirect_to new_user_path(:id => 1, :contact_id => 3, :name => 'suleman')
Upvotes: 34
Reputation: 2440
If you have some form data for example sent to home#action, now you want to redirect them to house#act while keeping the parameters, you can do this
redirect_to act_house_path(request.parameters)
Upvotes: 52
Reputation: 79
redirect_to :controller => "controller_name", :action => "action_name", :id => x.id
Upvotes: 7