SessionsHelper并未针对所有情况进行定义

时间:2014-03-03 20:39:06

标签: ruby-on-rails ruby ruby-on-rails-3

Ruby on Rails 3.2

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

2 个答案:

答案 0 :(得分:1)

尝试更改

distributor.find_by_remember_token(cookies[:remember_token])

到此

Distributor.find_by_remember_token(cookies[:remember_token])

答案 1 :(得分: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作为班级名称。