views:

34

answers:

3

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.email_preference setting as a default, then for editing the record use the foo.email_preference. Any suggestions?

A: 

Try Following

check_box_tag :preference, :email_preference, :value => preferences.email_preference
Salil
A: 

Try this:

value = @foo.new_record? ? preferences.email_preference : @foo.email_preference

f.check_box :email_preference, :value => value
j.
A: 

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.

Tilendor