I have a hidden form like this:
= form_for movie, 'create', method: 'POST' do |f|
- Movie.attribute_names.each do |attr|
= f.hidden_field attr.to_sym, value: movie.send(attr)
= f.submit 'Save this Movie', class: 'button'
Some of this attributes are serialized arrays, but padrino seems not to be parsing arrays from strings, so the db raises serialization error when I try to save a record from this attr hash stored in params:
{"title"=>"Iron Man 2",
"directors"=>"[\"Jon Favreau\"]",
"cast_members"=>"[\"Robert Downey Jr.\", \"Gwyneth Paltrow\", \"Don Cheadle\", \"Scarlett Johansson\", \"Sam Rockwell\"]",
"genres"=>"[\"Action\", \"Adventure\", \"Sci-Fi\"]",
"length"=>"124",
"trailer_url"=>"http://imdb.com/video/screenplay/vi1172179225/",
"year"=>"2010",
"languages"=>"[\"English\", \"French\", \"Russian\"]"}
I can parse the array string with this ugly chunk of code:
Movie.serialized_attributes.keys.each do |k|
movie[k] = movie[k].gsub(/\"|\[|\]/, '').split(', ') if movie[k]
end
but that seems just stupid, sinatra should be parsing it automatically (or not?), or maybe I'm generating the form the wrong way. How can I fix this behavior?
Note: just tried generating the form like this:
- Movie.attribute_names.each do |attr|
- if Movie.serialized_attributes.keys.include? attr
= f.hidden_field attr.to_sym, value: movie.send(attr), multiple: true
- else
= f.hidden_field attr.to_sym, value: movie.send(attr)