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.
Luca S. (Guest)
on 2007-02-16 14: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
Bill W. (Guest)
on 2007-02-16 16:10
(Received via mailing list)
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
Luca S. (Guest)
on 2007-02-16 16: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
Stephan W. (Guest)
on 2007-02-16 16:50
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
Luca S. (Guest)
on 2007-02-16 17: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
Russell N. (Guest)
on 2007-02-16 17:20
(Received via mailing list)
Try

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

RSL
Chris H. (Guest)
on 2007-02-16 17: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
Luca S. (Guest)
on 2007-02-16 17: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 N. wrote:
> Try
>
> redirect_to params.merge!(:action => :new_action)
>
> RSL
Bill W. (Guest)
on 2007-02-16 17:29
(Received via mailing list)
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
Russell N. (Guest)
on 2007-02-16 17: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.