Created
March 22, 2012 11:48
-
-
Save clm-a/2157877 to your computer and use it in GitHub Desktop.
Checkboxes for rendering an Hash in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file mood.rb | |
Mood = Struc.new(:happy) | |
# file user.rb | |
class User | |
serialize mood, Mood | |
def mood=(attr={}) | |
new_mood= Mood.new | |
new_mood.members.each do |k| | |
new_mood.send(:"#{k}=", attr.fetch(k, nil)) | |
end | |
super(new_mood) | |
end | |
end | |
# file users/edit.html.erb | |
<%= form_for user do |f| %> | |
<%= f.fields_for :mood, user.mood do |b| %> | |
<%= b.check_box :happy %> | |
<% end %> | |
<%= f.submit %> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment