ruby on rails - Edit a serialized hash in a form? -


i'm serializing hash stored in settings field in table, , able edit hash in form field.

class template < activerecord::base   serialize :settings end 

but <%= f.text_area :settings %> text area shows serialized data instead of hash.

how can hash show in text area?

maybe setting accessor model work.

class template < activerecord::base   serialize :settings   attr_accessor :settings_edit    before_save :handle_settings_edit, :if => lambda {|template| template.settings_edit.present? }    def settings_edit     read_attribute(:settings).inspect   # should display hash want   end    protected     def handle_settings_edit       # may want perform eval in validations instead of in        # before_save callback, can show errors on form.       begin         self.settings = eval(settings_edit)       rescue syntaxerror => e         self.settings = settings_edit       end     end   end 

then in form use <%= f.text_area :settings_edit %>.

i have not tested of code, in theory should work. luck!

warning: using eval dangerous, in example user delete entire template table 1 line in edit box template.destroy_all. use different method convert string hash if user input involved.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -