Ruby on rails 3 不确定如何使用rails 3复选框

Ruby on rails 3 不确定如何使用rails 3复选框,ruby-on-rails-3,Ruby On Rails 3,我有一个用户表单,如下所示,带有一个复选框,用于将用户设置为管理员: <%= form_for @user do |f| %> <%= render 'shared/error_messages', :object => f.object %> <div class="field"> <%= f.label :name %><br /> <%= f.text_fi

我有一个用户表单,如下所示,带有一个复选框,用于将用户设置为管理员:

   <%= form_for @user do |f| %>
      <%= render 'shared/error_messages', :object => f.object %>
      <div class="field">
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </div>
      <div class="field">
        <%= f.label :email %><br />
        <%= f.text_field :email %>
      </div>
      <div class="field">
        <%= f.label :password %><br />
        <%= f.password_field :password %>
      </div>
      <div class="field">
        <%= f.label :password_confirmation, "Confirmation" %><br />
        <%= f.password_field :password_confirmation %>
      </div>
        <%= hidden_field_tag :group_id, @group.id %>
      **<div class="field">
        <%= f.check_box :admin %> 
      </div>**
      <div class="actions">
        <%= f.submit "Sign up" %>
      </div>
    <% end %>
在我的用户控制器中,我的用户创建操作如下

def create
    @group = Group.find(params[:group_id])    
    @user = @group.users.build(params[:user])
    if @user.save
      flash[:success] = "You have created a new user"
      redirect_to groups_path
    else
      @title = "Sign up"
      render 'new'
    end
end 
但是当我测试user.admin是否为true时,它不会显示为true

<% @users.each do |user| %>
   <li>
     <%if user.admin == true %>
       <%= link_to user.name, user %>
     <% end%>
   </li>
<% end %>


  • 表单工作正常,否则,只有复选框没有将user.admin设置为true。我是否错过了某个关键步骤?谢谢

    您确定数据库中的值已设置为true吗?使用dbconsole检查它:

    rails dbsconsole
    
    如果没有,则您可能需要确保它在您的用户模型中是可访问的:

    attr_accessible :admin
    
    另外,作为旁注,ActiveRecord中的布尔值带有一个额外的问号方法,因此您可以这样做:

    if user.admin?
    

    attr_accessible:管理员是问题所在,非常感谢您的帮助我没有足够的声誉来提升投票率,但我已经接受了答案,谢谢
    if user.admin?