Setting Rails Checkbox from Another Model
I have a checkbox that belongs to "Foo" class. I have another "Preferences" class that sets the default for what that checkbox should be.
I tried using
f.check_box :email_preference, :value => preferences.email_preference
but it doesn't work. I use this page to do new record creation as well as edit, so obviously for new records I would want to take the preferences.e开发者_开发知识库mail_preference setting as a default, then for editing the record use the foo.email_preference. Any suggestions?
Try Following
check_box_tag :preference, :email_preference, :value => preferences.email_preference
Try this:
value = @foo.new_record? ? preferences.email_preference : @foo.email_preference
f.check_box :email_preference, :value => value
You're doing this in the wrong place. The view shouldn't care about the preferences class. When a new Foo is created, it should set the value of :email_preference on the object, and then the view will simply display the result of this.
I forget the name of the constructor method on ActiveRecord classes, or if there is a callback to leverage here. I'll look it up.
精彩评论