1

I have a Rails form with a few checkboxes. Here's the relevant part:

<div class="field">
    <%= f.label :roles %>
    <table>
      <tr><td>Admin</td><td>Sales</td><td>Moderator</td></tr>
      <tr>
        <td><%= f.check_box :is_admin? %></td>
        <td><%= f.check_box :is_sales? %></td>
        <td><%= f.check_box :is_moderator? %></td>
      </tr>
    </table>
</div>

This works, but ideally, instead of using is_admin? etc. I would like to use another method I wrote, check_role, which takes a string role name (e.g. 'admin').

def is_admin?
    self.check_role('admin')
end

# I'd like to use this method instead
def check_role(role_name)
    # checks the role name and returns true or false
end

I'm not sure how to do that because the check_role method takes 1 argument, but the documentation and all the examples I've viewed for the form helper check_box always use a method that takes zero arguments.

Is this possible using Rails form helpers, or am I stuck with using only zero argument methods?

4

1 回答 1

0

医生说:

It’s intended that method returns an integer and if that integer is above zero...

尝试让你的方法返回整数

于 2013-07-03T03:33:31.113 回答