# How to pass params from an action to another you redirect\_to

**URL:** <https://www.ruby-forum.com/t/how-to-pass-params-from-an-action-to-another-you-redirect-to/66846>\
**Category:** Rails\
**Created:** [August 29, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/how-to-pass-params-from-an-action-to-another-you-redirect-to/66846 "2006-08-29T19:27:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [August 29, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/how-to-pass-params-from-an-action-to-another-you-redirect-to/66846/1 "2006-08-29T19:27:33Z")

</div>

Suppose action foo #redirects\_to action bar. Once #redirected\_to, bar  
would not have action foo’s params.

One simple solution is:

params[:action] = ‘bar’  
redirect\_to(params)

With that params is preserved in bar. The problem is that the HTTP  
method of bar is GET, so the params are shown in the URL.

Any better solution?

More broadly, any other way to pass data between foo and bar, besides  
sticking it in session?

-Alder

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [August 29, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/how-to-pass-params-from-an-action-to-another-you-redirect-to/66846/2 "2006-08-29T20:08:07Z")

</div>

That’s one of the chief reasons for the session… to persist parameters  
between requests without using the URL. I used this method all the time  
in  
my old ASP applications. At least Rails makes it easy

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [August 29, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/how-to-pass-params-from-an-action-to-another-you-redirect-to/66846/3 "2006-08-29T20:14:09Z")

</div>

On 29-Aug-06, at 1:27 PM, Alder G. wrote:

> More broadly, any other way to pass data between foo and bar, besides  
> sticking it in session?

I think the session is your only bet. Why not store the params in the  
flash? That way you won’t have to worry about manually expiring it  
like you would if you created your own session.

flash[:params] = params

/Jeff

–  
[http://re.visioni.st](http://re.visioni.st)  
[http://quotedprintable.com](http://quotedprintable.com)

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [August 29, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/how-to-pass-params-from-an-action-to-another-you-redirect-to/66846/4 "2006-08-29T21:46:31Z")

</div>

Use a before filter to pull the data into an instance variable from the  
session if the session has the key. Then invoke this filter on any  
methods  
that have multiple entrypoints.

before\_filter :get\_parameter\_data, :only =\>[:create, :update]

def create  
@bar = Bar.new(@p[:bar])  
@if bar.save  
…  
end

def update

end

private

# could even make this alter params[] directly if you really wanted

to… no  
reason why not  
def get\_parameter\_data  
if session[:params].nil?  
@p = params  
else  
@p = session[:params]  
session[:params] = nil  
end  
end

How does that work foyou? Or am I not quite understanding this?

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [August 29, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/how-to-pass-params-from-an-action-to-another-you-redirect-to/66846/5 "2006-08-29T20:48:18Z")

</div>

Brian H. wrote:

> That’s one of the chief reasons for the session… to persist parameters  
> between requests without using the URL. I used this method all the time in  
> my old ASP applications. At least Rails makes it easy

Ok, but what would you do in the following common scenario:

#bar requires two parameters, bart and lisa. Normally, #bar is invoked  
directly, by a POST request supplying it with those parameters. But  
sometimes, you want to #redirect\_to it from some other method, say  
#foo.

One option is to write a mechanism at the beginning of the method which  
abstracts bart and lisa (e.g. assigns them to local variables) so that  
the rest of the method is indifferent as to whether bart and lisa were  
supplied by a direct POST or by passed in the session.

This is awkward and the result will be somewhat bloated imho.

Another option is to transfer all of #bar’s functionality to a new  
method, #baz, that is ALWAYS invoked indirectly, by a #redirect\_to, so  
it ALWAYS looks for bart and lisa in the session. Then of course you  
make the original #bar a thin wrapper that takes bart and lisa from  
params, sticks them in session where #baz would find them, and  
redirects\_to #baz.

This latter one is more elegant, but I wonder if all those redirects  
are really a Good Idea, considering performance issues and such.

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [July 23, 2008, 8:46pm UTC](https://www.ruby-forum.com/t/how-to-pass-params-from-an-action-to-another-you-redirect-to/66846/6 "2008-07-23T20:46:25Z")

</div>

Hi

I am looking to a similar sort of thing.  
Did you find a solution to it other than using session?

Thanks

Ruhul
