Add 'params' variable to redirect_to(...)

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

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

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

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

I get the feeling we’re almost there :slight_smile:

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

undefined method `join’ for #HashWithIndifferentAccess:0xb756b7d8

Any suggestions why ?

Thnx
LuCa

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

Try

redirect_to params.merge!(:action => :new_action)

RSL

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

How can I do a redirect_to which results into the following URL

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

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

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