Forum: Ruby on Rails add 'params' variable to redirect_to(...)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2007-02-16 13:21
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-02-16 15:10
(Received via mailing list)
Hi Luca,

Luca Scaljery 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
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2007-02-16 15:43
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
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2007-02-16 15:50
Luca Scaljery 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
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2007-02-16 16:15
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
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-02-16 16:20
(Received via mailing list)
Try

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

RSL
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-02-16 16:27
(Received via mailing list)
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
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2007-02-16 16:28
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=...........

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 Norris wrote:
> Try
>
> redirect_to params.merge!(:action => :new_action)
>
> RSL
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-02-16 16:29
(Received via mailing list)
Hi Luca,

Luca Scaljery 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
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-02-16 16:34
(Received via mailing list)
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
This topic is locked and can not be replied to.