Here’s my view:
<%= check_box_tag :age_visible, 1, checked?(:age_visible) %>
This accesses the helper method:
def checked?(pref)
if @user.preferences.pref == "1"
true
else
false
end
end
I want the helper to state:
if @user.preferences.age_visible == "1"
so it will check the database to see if the user has previously chosen to show their age and if so the check box will be checked; otherwise, not. Right now it simply passes false, but will pass true if I write out “@user.preferences.age_visible” in the helper. But, of course, I want to be able to use the helper for other attributes.
Davidb’s answer is correct, but for future cases where you want to call a method dynamically, you can use
send. E.g. like this:Just a little tip to make your helper more concise:
already returns a boolean, so you can rewrite your helper like this: