Passing parameters in rails redirect_to
Asked Answered
C

11

222

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
Celestyn answered 16/9, 2009 at 0:2 Comment(2)
What are you actually trying to accomplish? Have you considered saving the data in the session?Fariss
What you're asking for is not possible -- if you're doing a redirect, it must be a GET request that you're redirecting to, so the params will always be visible to your users. You should store stuff in the session instead.Brandie
F
188

Just append them to the options:

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

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

Fariss answered 16/9, 2009 at 0:31 Comment(2)
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.Franks
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 :))Celestyn
C
92

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')
Cheslie answered 3/1, 2010 at 17:57 Comment(0)
R
52

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)

Redouble answered 8/8, 2012 at 17:24 Comment(3)
Do you know how you would combine this, plus new parameters? i.e. request.parameters, (or +) :this => :thatBootee
You could use the merge method. For example: redirect_to act_house_path(request.parameters.merge(key: "value"))Brandie
You could also use the except method to exclude unwanted params after merging: redirect_to act_house_path(request.parameters.except(:authenticity_token, :controller, :action, :utf8, :commit)Adversaria
E
39

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| %>
Execratory answered 8/4, 2011 at 2:48 Comment(1)
This is poor practice. This method misuses the flash object, its intended for user messaging. Better would be to store arbitrary parameters in the session and then clear those parameters from the session when you're done.Brandie
M
33
redirect_to new_user_path(:id => 1, :contact_id => 3, :name => 'suleman')
Moir answered 26/8, 2013 at 9:54 Comment(0)
R
10

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.

Rigel answered 1/11, 2018 at 21:56 Comment(0)
T
7
redirect_to :controller => "controller_name", :action => "action_name", :id => x.id
Tropicalize answered 17/2, 2012 at 11:7 Comment(1)
Can you please provide some more explanation. Just a code example is really not enough.Prisoner
A
7

Route your path, and take the params, and return:

redirect_to controller: "client", action: "get_name", params: request.query_parameters and return
Ataliah answered 22/5, 2019 at 14:4 Comment(0)
U
6

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" 
Upheld answered 16/8, 2014 at 7:36 Comment(0)
L
5

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'

Lutetium answered 7/12, 2020 at 12:33 Comment(0)
J
0

As a polymorphic route...

redirect_to [@mything, {key: 'value'}]

or

redirect_to [:mythings, {key: 'value'}]

etc.

Joshia answered 5/4 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.