# Passing Values Between Action Methods

**URL:** <https://www.ruby-forum.com/t/passing-values-between-action-methods/122910>\
**Category:** Rails\
**Created:** [December 12, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/passing-values-between-action-methods/122910 "2007-12-12T21:01:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 12, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/passing-values-between-action-methods/122910/1 "2007-12-12T21:01:16Z")

</div>

I have 2 action methods, action1 and action2. action1 is always run  
before action2. Short of using the dreaded global variable, how can I  
access a value developed in action1 from action2? Thanks for any  
input.

```
   ... doug
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 12, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/passing-values-between-action-methods/122910/2 "2007-12-12T21:07:17Z")

</div>

If you are redirecting to action2 at the end of action1, just append  
the value to the end of the redirect:

my\_var =   
redirect\_to :action =\> ‘action2’, :my\_var =\> my\_var

should work.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 12, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/passing-values-between-action-methods/122910/3 "2007-12-12T21:07:17Z")

</div>

On Dec 13, 2007 1:30 AM, doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have 2 action methods, action1 and action2. action1 is always run  
> before action2. Short of using the dreaded global variable, how can I  
> access a value developed in action1 from action2? Thanks for any  
> input.

u can send a value like this  
def action1  
redirect\_to :action =\> ‘action2’, :value =\> params[:current\_varaible]  
end  
def action2  
puts params[:value].inspect  
end

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 12, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/passing-values-between-action-methods/122910/4 "2007-12-12T22:31:40Z")

</div>

> If you are redirecting to action2 at the end of action1, just append  
> the value to the end of the redirect:

Thanks to both responders. I appologize for not making clear that I’m  
not redirecting from action1 to action2. Actually this is a checkout  
system where action1 redirects to a checkout handler like PayPal and  
then after the checkout handler does its thing, we redirect to a  
fulfillment handler to complete the order. The bottom line is that  
action1 does not “call” action2; so, I can’t pass the information in  
the call. That would obviously make life much easier.

Thanks for the input. Am I stuck with the dreaded global variable?

```
    ... doug
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 13, 2007, 12:40am UTC](https://www.ruby-forum.com/t/passing-values-between-action-methods/122910/5 "2007-12-13T00:40:11Z")

</div>

> (even if the need for  
> the data is “temporary”, you do need to handle concurrent users,  
> right?).

Yep and I think that alone dictates what I’m going to have to do.  
Thanks for helping me focus my thinking.

```
   ... doug
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 12, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/passing-values-between-action-methods/122910/6 "2007-12-12T22:45:44Z")

</div>

On Dec 12, 2007, at 4:30 PM, doug wrote:

> Thanks for the input. Am I stuck with the dreaded global variable?
> 
> ```
> ... doug
> 
> ```

A global variable is probably not going to help you anyway.

If this “order” is in a database, then you may want to link this data  
that you need in action2 to the order as an attribute in the same  
table or in a new table the belongs\_to :order (even if the need for  
the data is “temporary”, you do need to handle concurrent users,  
right?).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
