# How can I pass Ruby object to another action?

**URL:** <https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231>\
**Category:** Rails\
**Created:** [August 5, 2009, 10:57am UTC](https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231 "2009-08-05T10:57:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 5, 2009, 10:57am UTC](https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231/1 "2009-08-05T10:57:04Z")

</div>

I use form\_tag like below:

\<% form\_tag (my\_action) %\>

\<%= submit\_tag “Submit” %\>

\<% end %\>

In this case, how can I pass a Ruby object to “my\_action” action? I want  
to pass a Hash to the next action.

thanks.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 5, 2009, 12:19pm UTC](https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231/2 "2009-08-05T12:19:22Z")

</div>

Zhao Yi wrote:

> I use form\_tag like below:
> 
> \<% form\_tag (my\_action) %\>
> 
> \<%= submit\_tag “Submit” %\>
> 
> \<% end %\>
> 
> In this case, how can I pass a Ruby object to “my\_action” action? I want  
> to pass a Hash to the next action.
> 
> thanks.

You can’t pass ruby objects from the form to the controller because the  
browser has no concept of ruby or rails or any of that stuff. However,  
you can build a hash to put into params like this

\<%= hidden\_field\_tag “foo[bar]”, “hello” %\>

which creates this situation

params =\> {:foo =\> {:bar =\> “hello”}}

Knowing this, you can make a hidden field tag, or set of hidden field  
tags, which recreates the hash you want to pass through. Ultimately,  
this is all that a form does: it builds a data structure inside params,  
which is a hash that can contain other hashes, strings, and arrays.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 5, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231/3 "2009-08-05T22:22:38Z")

</div>

How about saving the object in a session value?

session[:my\_object] = my\_object/@my\_object

The next action could just retrieve it easily:

my\_object/@my\_object = session[:my\_object]

Pepe

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 6, 2009, 10:22am UTC](https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231/4 "2009-08-06T10:22:19Z")

</div>

Zhao Yi wrote:

> Max W. wrote:
> 
> > You can’t pass ruby objects from the form to the controller because the  
> > browser has no concept of ruby or rails or any of that stuff. However,  
> > you can build a hash to put into params like this
> > 
> > \<%= hidden\_field\_tag “foo[bar]”, “hello” %\>
> > 
> > which creates this situation
> > 
> > params =\> {:foo =\> {:bar =\> “hello”}}
> > 
> > Knowing this, you can make a hidden field tag, or set of hidden field  
> > tags, which recreates the hash you want to pass through. Ultimately,  
> > this is all that a form does: it builds a data structure inside params,  
> > which is a hash that can contain other hashes, strings, and arrays.
> 
> But when I get the hash object from params, I get a String object  
> instead of a hash object.

Post up the form and the contents of your params hash please - you can  
see this in your log file.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 6, 2009, 10:27am UTC](https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231/5 "2009-08-06T10:27:06Z")

</div>

pepe wrote:

> How about saving the object in a session value?
> 
> session[:my\_object] = my\_object/@my\_object
> 
> The next action could just retrieve it easily:
> 
> my\_object/@my\_object = session[:my\_object]
> 
> Pepe

This isn’t what the session is for. It’s bad practise imo to use the  
session as a storage space for any arbitrary data - it’s going to fill  
up with a load of junk real quickly. What if two different actions  
happen to use the same session variable name? What if you want to get  
to step two of the process from somewhere else, that didn’t save the  
session variable? It’s just asking for trouble. Keep the session data  
to an absolute minimum.

If you’re doing something based on the submission of a form, then that  
should depend on what the form sends through, not on the form and some  
stuff that you hope is in the session. That way you can look at the  
form and see what’s going on, without knowing about some mysterious  
session stuff.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 6, 2009, 2:55am UTC](https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231/6 "2009-08-06T02:55:22Z")

</div>

Max W. wrote:

> You can’t pass ruby objects from the form to the controller because the  
> browser has no concept of ruby or rails or any of that stuff. However,  
> you can build a hash to put into params like this
> 
> \<%= hidden\_field\_tag “foo[bar]”, “hello” %\>
> 
> which creates this situation
> 
> params =\> {:foo =\> {:bar =\> “hello”}}
> 
> Knowing this, you can make a hidden field tag, or set of hidden field  
> tags, which recreates the hash you want to pass through. Ultimately,  
> this is all that a form does: it builds a data structure inside params,  
> which is a hash that can contain other hashes, strings, and arrays.

But when I get the hash object from params, I get a String object  
instead of a hash object.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 6, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/how-can-i-pass-ruby-object-to-another-action/173231/7 "2009-08-06T15:30:31Z")

</div>

What about initializing the object with the params values?

Something like

foo = MyObject.new(params[:foo])

On Aug 6, 9:27 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
