Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 管理不';不要使用Mongo Mapper_Ruby On Rails 3_Mongodb_Mongomapper - Fatal编程技术网

Ruby on rails 3 管理不';不要使用Mongo Mapper

Ruby on rails 3 管理不';不要使用Mongo Mapper,ruby-on-rails-3,mongodb,mongomapper,Ruby On Rails 3,Mongodb,Mongomapper,我尝试使用MongoDb,并使用Rails教程创建用户。我正在使用Mongo_地图绘制器宝石。我已经设置好了所有这些,但我已经到了ActiveRecord函数/方法似乎不起作用的地步。我所有的测试都进行到这一点 我有开关!通过在APIDock中查找切换源,将其添加到我的用户模型中 def toggle!(field) send "#{field}=", !self.send("#{field}?") save end 但是当我尝试使用这个函数来查看用户模型是否为adm

我尝试使用MongoDb,并使用Rails教程创建用户。我正在使用Mongo_地图绘制器宝石。我已经设置好了所有这些,但我已经到了ActiveRecord函数/方法似乎不起作用的地步。我所有的测试都进行到这一点

我有开关!通过在APIDock中查找切换源,将其添加到我的用户模型中

  def toggle!(field)
    send "#{field}=", !self.send("#{field}?")
    save 
  end
但是当我尝试使用这个函数来查看用户模型是否为admin

  def admin?
    self.admin
  end
我从测试中得到这个错误

 1) UsersController DELETE 'destroy' as a non-signed-in user should deny access
     Failure/Error: delete :destroy, :id => @user
     NoMethodError:
       undefined method `admin?' for nil:NilClass
     # ./app/controllers/users_controller.rb:66:in `admin_user'
     # ./spec/controllers/users_controller_spec.rb:253:in `block (4 levels) in <top (required)
1)用户控制器删除“destroy”,因为未登录用户应拒绝访问
失败/错误:delete:destroy,:id=>@user
命名错误:
nil:NilClass的未定义方法“admin”
#/app/controllers/users\u controller.rb:66:in'admin\u user'
#./spec/controllers/users\u controller\u spec.rb:253:in`block(4层)中的“You sure?”,
:title=>“删除#{user.name}”%>


关于如何修复此问题的任何建议,因为我一直在思考如何在删除用户后使应用程序重定向。

由于您在测试期间没有登录用户,
当前用户
nil
,这正是您遇到的错误

因此,您需要处理
当前用户
为零的情况:

def admin_user
  redirect_to(root_path) unless current_user.try(:admin?)
end
try
方法将处理在nil对象上调用它的情况

<li>
    <%= link_to user.name, user %>
    <% if current_user.admin? %>
        <%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
                                    :title => "Delete #{user.name}" %>
    <% end %>
</li>
def admin_user
  redirect_to(root_path) unless current_user.try(:admin?)
end