2014-03-03 79 views
0

Ruby on Rails的3.2SessionsHelper沒有確定對所有情況

我SessionsHelper正在爲我的車型之一,但不是其他。我無法弄清楚這個問題。我得到他的錯誤:

NameError (undefined local variable or method `distributor' for #<DistributorsController:0x00000005357c58>): 
app/helpers/sessions_helper.rb:55:in `current_distributor' 
app/helpers/sessions_helper.rb:59:in `current_distributor?' 
app/controllers/distributors_controller.rb:138:in `correct_distributor' 

這些都是在我的幫手中定義的。我的ApplicationController有include SessionsHelper。問題是什麼?

這是我的助手的一部分:

def sign_in_distributor(distributor) 
    cookies[:remember_token] = distributor.remember_token 
    self.current_distributor = distributor 
end 

def signed_in_distributor? 
    !current_distributor.nil? 
end 

def current_distributor=(distributor) 
    @current_distributor = distributor 
end 

def current_distributor 
    @current_distributor ||= distributor.find_by_remember_token(cookies[:remember_token]) 
end 

def current_distributor?(distributor) 
    distributor == current_distributor 
end 

def sign_out_distributor 
    self.current_distributor = nil 
    cookies.delete(:remember_token) 
end 

def admin_distributor 
    redirect_to(current_distributor) unless current_distributor.admin? 
end 

def admin_distributor? 
    current_distributor.admin? 
end 

這是我的控制器:

class DistributorsController < ApplicationController 

before_filter :signed_in_grandstreamer, only: [:edit, :update, :show, :index] 
before_filter :correct_distributor, only: [:edit, :update, :show, :change_password] 
before_filter :admin_distributor,  only: [:index, :new, :edit, :destroy] 

def index 
@distributors = Distributor.paginate(page: params[:page]) 

redirect_to root_url 
end 

def new 
if signed_in_distributor? 
    redirect_to distributor_path(current_distributor) 
elsif signed_in_grandstreamer? 
    redirect_to grandstreamer_path(current_grandstreamer) 
else  
    @distributor = Distributor.new 
end 
end 

def show 
#@distributor = Distributor.find(params[:id]) 
end 

...skipping some... 

private 

def signed_in_grandstreamer 
    unless signed_in_grandstreamer? 
     store_location 
     redirect_to signin_url, notice: "Please sign in." unless signed_in_grandstreamer? 
    end 
    end 

    def signed_in_distributor 
    unless signed_in_distributor? 
     store_location 
     redirect_to signin_url, notice: "Please sign in." unless signed_in_distributor? 
    end 
    end 

    def correct_distributor 
    @distributor = Distributor.find(params[:id]) 
    #@user = User.find(current_user.id) 
    @checkuser = Distributor.find(params[:id]) 
    redirect_to(current_distributor) unless current_distributor?(@checkuser) || admin_distributor? 
    end 
end 

回答

1

我認爲你需要改變這個幫手:

def current_distributor 
    @current_distributor ||= distributor.find_by_remember_token(cookies[:remember_token]) 
end 

要:

def current_distributor 
    @current_distributor ||= Distributor.find_by_remember_token(cookies[:remember_token]) 
end 

使用CamelCase的類名。

+1

+1關於CamelCase的好消息。 – DDDD

1

嘗試改變

distributor.find_by_remember_token(cookies[:remember_token]) 

對此

Distributor.find_by_remember_token(cookies[:remember_token]) 
+0

+1正確答案,但第二個答案。 – DDDD

相關問題