How can I pass Ruby object to another action?

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.

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.

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

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.

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.

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.

What about initializing the object with the params values?

Something like

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

On Aug 6, 9:27 am, Max W. [email protected]