# Add 'params' variable to redirect\_to(...)

**URL:** <https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805>\
**Category:** Rails\
**Created:** [February 16, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805 "2007-02-16T13:21:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/1 "2007-02-16T13:21:01Z")

</div>

Hi All

I have a HTML-form, that when submitted calles the action called ‘abc’.  
But abc needs to redirect\_to a different action, passing to it the  
‘params’ variable.

Any suggestions how to use redirect\_to passing it the params variable ?

Thnx a lot  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/2 "2007-02-16T15:10:49Z")

</div>

Hi Luca,

Luca S. wrote:

> I have a HTML-form, that when submitted calles  
> the action called ‘abc’. But abc needs to redirect\_to  
> a different action, passing to it the ‘params’ variable.
> 
> Any suggestions how to use redirect\_to passing it  
> the params variable ?

I’m not sure which of 2 problems you’re having, but both have short  
answers  
so …

To pass parameters with redirect\_to you simply add them. Like …  
redirect\_to :controller =\> ‘another’, :action =\> ‘def’, :param1 =\>  
‘some’,  
:param2 =\> ‘thing’, :param =\> ‘else’

I’m guessing you’re asking about the fact that parameters only ‘live’  
for  
one request-response cycle. redirect\_to ends one cycle and starts  
another  
so, in the situation you’ve outlined, you’ll need to save them to (a)  
local  
variable(s) in the first method, then add them to the redirect\_to to  
receive  
them in the second. Of course, you could also store them in the session  
or  
in the database too.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/3 "2007-02-16T15:43:03Z")

</div>

Hi Bill

What happens when you don’t know what varaibles ‘params’ contains, so  
you want to pass all of them with the redirect ?

Something like: redirect\_to ( :action =\> ‘blabla’, params )

In your example you already know which params to pass on!

LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/4 "2007-02-16T15:50:39Z")

</div>

Luca S. wrote:

> Hi Bill
> 
> What happens when you don’t know what varaibles ‘params’ contains, so  
> you want to pass all of them with the redirect ?

How about

redirect\_to params.join :action =\> ‘new\_action’

Stephan

> Something like: redirect\_to ( :action =\> ‘blabla’, params )
> 
> In your example you already know which params to pass on!
> 
> LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/5 "2007-02-16T16:15:16Z")

</div>

I get the feeling we’re almost there 🙂

When I add the params.join I get an error:

undefined method `join’ for #HashWithIndifferentAccess:0xb756b7d8

Any suggestions why ?

Thnx  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/6 "2007-02-16T16:27:15Z")

</div>

this would work, however, there is the problem of nested params, such  
as what is submitted from a form using form tag helpers where rails  
handles naming the form fields and thus you could end up with a params  
hash such as

{ “user” =\> { “name” =\> “bob” } }

this would not work as it would end up with a query string like  
?user=namebob in the redirect url instead of ?user[name]=bob

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/7 "2007-02-16T16:20:13Z")

</div>

Try

redirect\_to params.merge!(:action =\> :new\_action)

RSL

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/8 "2007-02-16T16:28:17Z")

</div>

thnx, thats the trick.

One last thing, using a redirect\_to as you suggested, I get an URL that  
looks like

[http://blabla.bla/my\_controller/new\_action?ksdhkv=dgwg&skd](http://blabla.bla/my_controller/new_action?ksdhkv=dgwg&skd)…

How can I do a redirect\_to which results into the following URL

[http://blabla.bla/my\_controller/new\_action](http://blabla.bla/my_controller/new_action)

Does that not have to do with GET and/or POST ?

LuCa

Russell N. wrote:

> Try
> 
> redirect\_to params.merge!(:action =\> :new\_action)
> 
> RSL

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/9 "2007-02-16T16:29:18Z")

</div>

Hi Luca,

Luca S. wrote:

> What happens when you don’t know what  
> varaibles ‘params’ contains, so you want to  
> pass all of them with the redirect ?

I don’t understand this question. The params hash will contain keys for  
every element in the form you submitted. Some may have an empty string  
for  
their value, but they’ll all be there. If you only want to pass along  
the  
ones that have values, then you’ll have to examine them before passing  
them  
along.

BTW… according to Pickaxe, join is not a defined method on a Hash.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 16, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/add-params-variable-to-redirect-to/85805/10 "2007-02-16T16:34:20Z")

</div>

Yeah. That’s a HTTP method issue there. You might not want to pass wall  
the  
params there ‘cause it seems to be breaking your routes’ styling. Or  
just  
bite the bullet on that ugly URL. I’d opt for the former but that’s me.

RSL
