How to serialize a symbol coming from a Form

Hello, I would like to use serialized symbols as values for select in
forms.

eg.
class Vehicle < ActiveRecord::Base
@@statuses = {:wait => ‘No yet’, :ok => ‘Run run run’}
validates_inclusion_of status, :in => @@statutes.keys
Serialize status, Symbol

def self.statuses
@@statuses
end
end

Then, I can use Vehicle.statuses.invert to populate my select drop down
list in the form.

Unfortunately when the form is submited the ‘values’ sent are not
recognized as a symbols and an error occurs
“status was supposed to be a Symbol, but was a String”

I understand that the value posted is “wait” or “ok” and not :wait or
:ok but I didn’t found a way to convert them to symbols correctly.

in the controller using
vehicle.status = eval(":#{vehicle.status}")

Of course I might use strings as keys instead of symbols for the hashes
but I find them less readable in the source code.

Any help appreciated, thanks

I wanted to write :
in the controller using
vehicle.status = eval(":#{vehicle.status}")

didn’t helped

The code above is not real, forgive the typos

nuno wrote:

I wanted to write :
in the controller using
vehicle.status = eval(":#{vehicle.status}")

didn’t helped

The code above is not real, forgive the typos

Hi Nuno,

Here is the syntax:
“foo”.to_sym

Jean-Etienne

Fiiiiine !!

Merci / Thank you Jean Etienne !

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs