Passing Values Between Action Methods

77 views
Skip to first unread message

doug

unread,
Dec 12, 2007, 3:00:35 PM12/12/07
to Ruby on Rails: Talk
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

Phillip Koebbe

unread,
Dec 12, 2007, 3:06:37 PM12/12/07
to rubyonra...@googlegroups.com
If you are redirecting to action2 at the end of action1, just append
the value to the end of the redirect:

my_var = <some logic>
redirect_to :action => 'action2', :my_var => my_var

should work.

Peace,
Phillip

bala kishore pulicherla

unread,
Dec 12, 2007, 3:06:41 PM12/12/07
to rubyonra...@googlegroups.com


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 

      ... doug

doug

unread,
Dec 12, 2007, 4:30:58 PM12/12/07
to Ruby on Rails: Talk
> 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

Rob Biedenharn

unread,
Dec 12, 2007, 4:39:00 PM12/12/07
to rubyonra...@googlegroups.com

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 Biedenharn http://agileconsultingllc.com
R...@AgileConsultingLLC.com


doug

unread,
Dec 12, 2007, 6:39:17 PM12/12/07
to Ruby on Rails: Talk
> (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
Reply all
Reply to author
Forward
0 new messages