February 21st, 2008, 02:08 PM
Calling model methods even when the param is a not set
I'm setting a checkbox values based on the return of a model field method. The way the method is set up - it only gets called when a checkbox value is set.
Here's the model code:
I would like that method to be called even when the checkbox is not set.
@within = new_within == "1" || new_within == true || new_within == 'true' || new_within == 'on'
Thanks for any help anyone can provide.
February 23rd, 2008, 05:24 PM
You're not going to get that method called when the value is not set in the checkbox. That's because the HTTP protocol will not send the field value if the checkbox is not set. The solution is to
1. Set the underlying model's default value to 0 for the field, so when you create a new instance, the default is set.
2. Check if model.within is set after you've set the other values and then set it yourself, if it isn't:
3. Override the new method for your model and make it set the value of model.within first before updating with all the passed in params.
model = Model.new(params[:model])
model.within = 0
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo