2011-10-12 5 views
0

MongoDb를 사용하고 Rails 자습서를 사용하여 사용자를 만들려고했습니다. Mongo_mapper gem을 사용하고 있습니다. 그 모든 설정을 가지고 있지만 ActiveRecord 함수/메서드가 작동하지 않는 것 같습니다. 모든 테스트가이 시점까지 진행됩니다.admin? Mongo Mapper와 작동하지 않습니다.

나는 토글했다! APIDock

def toggle!(field) 
    send "#{field}=", !self.send("#{field}?") 
    save 
    end 

에 토글 소스를보고 내 사용자 모델이 추가하지만이 기능을하려고 할 때 관리자의 사용자 모델 부울

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) 

는 여기에 내가 컨트롤러

class UsersController < ApplicationController 
    before_filter :authenticate, :only => [:index, :edit, :update] 
    before_filter :correct_user, :only => [:edit, :update] 
    before_filter :admin_user, :only => :destroy 
. 
. 
. 

    def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User deleted." 
    redirect_to users_path 
    end 

    private 

    def authenticate 
     deny_access unless signed_in? 
    end 

    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_path) unless current_user?(@user) 
    end 

    def admin_user 
     redirect_to(root_path) unless current_user.admin? 
    end 

위해 가지고 무엇을하지만 내 관리자 사용자와 작업 관리자 권한을받을 수 있나요 나는 사용자를 삭제 할 수 있어요.

<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> 

I 앱이 사용자를 삭제 한 후 리디렉션하는 방법에 붙어로이 문제를 해결하는 방법에 대한 어떤 제안.

답변

1

해당 테스트 중에 로그인 한 사용자가 없기 때문에 current_usernil입니다. 이는 정확히 귀하가받는 오류입니다.

그래서 당신은 current_user이 전무가 어디에 경우를 처리해야합니다

def admin_user 
    redirect_to(root_path) unless current_user.try(:admin?) 
end 

그것이 전무 객체에서 호출되는 경우를 처리 할 try 방법.

+0

감사합니다. @ctcherry! 그게 효과가있어. – zigloo99

관련 문제