It's designed to be used in a Rails helper. You can either pass it the object, method, and value (the same parameters as, for example, radio_button) or name and value (the same parameters as radio_button_tag).
def checked?( *args ) if args.length == 3 object, method, value = args if params[object] && params[object][method] && params[object][method] == value 'checked' end elsif args.length == 2 name, value = args if params[name] && params[name] == value true end end end
Here's an example usage:
<%= radio_button 'person', 'age', '12', :checked => checked?( 'person', 'age', '12' ) %>
If params[:person][:age] exists and it equals '12', then 'checked?' returns 'checked'; otherwise, it returns nil.
Where would you add this code? Not on the model (I'm using it without a model using radio_button_tag), and it doesn't seem to work in the controller. Where should I place this?
Thanks
reinier