【问题标题】:How do you create a current_user method without using devise如何在不使用设计的情况下创建 current_user 方法
【发布时间】:2014-10-28 07:47:15
【问题描述】:

我想创建一个 Current_User 方法,但我不想使用 gem 或类似方法。我将如何在 Rails 4.1.2 中做到这一点

Questions_Controller 我想要 current_user 方法的地方。

class QuestionsController < ApplicationController
before_filter :auth, only: [:create, :your_questions, :edit, :update]

  # def index
  #     @question = Question.new
  #   @questions = Question.unsolved(params)
  # end

  @questions = current_user.your_questions(params[:page])

  def your_questions(page)
  questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
  end


  def self.unsolved(params)
    order('created_at DESC').where(solved: false).paginate(page: params[:page],per_page: 3)
  end

  def create
    @question = current_user.questions.build(params[:question])
    if @question.save
        flash[:success] = 'Your question has been posted!'
        redirect_to @question
    else
      @questions = Question.unsolved(params)
        render 'index'
    end
  end

  def new
       @question = Question.new
  end

  def show
    # raise FOO
    puts params
    @question = Question.find(params[:id])
    @answer = Answer.new
  end

  def your_questions
    @questions = current_user.your_questions(params[:page])
    # current_user.your_questions(params[:id])
  end

  def edit
    @question = current_user.questions.find(params[:id])
  end

  def update
    @question = current_user.questions.find(params[:id])

    if @question.update_attributes(params[:question])
      flash[:success] = 'Your question has been updated!'
      redirect_to @question
    else
      render 'edit'
    end
  end

  def search
    @questions = Question.search(params)
  end
end

我的用户模型

    class User < ActiveRecord::Base
    has_many :questions
    has_many :answers
  # attr_accessible :username, :password, :password_confirmation

  has_secure_password

  # validates :username, presence: true, uniqueness: { case_sensitive: false },
  #                                          length: { in: 4..12 },
  #                                          format: { with: /A[a-z][a-z0-9]*z/, message: 'can only contain lowercase letters and numbers' }
    validates :password, length: { in: 4..8 }
    validates :password_confirmation, length: { in: 4..8 }

    def your_questions(page)
    questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
  end
end

我的应用程序控制器

    class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  # protect_from_forgery

  helper_method [:current_user, :logged_in?]

  protected

  private 

    def login(user)
        session[:user_id] = user.id
    end

    def current_user
        current_user ||= User.find(session[:user_id]) if session[:user_id]
    end

    def logged_in?
        !current_user.nil?
    end

    def auth
      redirect_to login_url, alert: 'You must login to access that page' unless logged_in?
    end
end

如果您希望我将更多文件添加到问题中,请评论我是 Rails 开发人员的新手 ruby​​ :)

【问题讨论】:

标签: ruby-on-rails ruby


【解决方案1】:
class ApplicationController < ActionController::Base

 helper_method :current_user 

 private

 def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
 end

end

【讨论】:

  • 我已经在我的应用程序控制器中拥有了它,但是我添加了私有但我已经保护了,但是当我重新加载它时它给出了相同的错误,即“未定义的局部变量或方法 `current_user' for QuestionsController:类”
  • 在代码中附上总结其本质的文本通常是一件好事。在这种情况下,很难看出有什么不同。
【解决方案2】:

我认为您的问题不在于控制器,而在于您的助手。

它正在调用正在寻找局部变量current_user 的方法,但没有。您需要像这样实例化这些:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  helper_method [:current_user, :logged_in?]


  private 

    def login(user)
      session[:user_id] = user.id
    end

    def current_user
       @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end

    def logged_in?
      !current_user.nil?
    end

    def auth
      redirect_to login_url, alert: 'You must login to access that page' unless logged_in?
    end
end

喜欢@RafalG。声明见current_user前面的@。这将创建一个实例变量来跟踪,而不是引用丢失的局部变量。

还请注意,您当前的 current_user 方法将始终运行 User.find,因为局部变量 current_user 在此范围内将始终为 nil,因此您需要使其成为实例的一部分。

更新

我将把上面的内容留待启发,因为您仍然应该创建一个实例。我认为这是真正的问题

class QuestionsController < ApplicationController
  before_filter :auth, only: [:create, :your_questions, :edit, :update]

  # def index
  #   @question = Question.new
  #   @questions = Question.unsolved(params)
  # end

  #vvvv This Line is out of a scope and will raise errors vvv#
  @questions = current_user.your_questions(params[:page])

  def your_questions(page)
    questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
  end
  ....
end

如果您想这样做,您可以在before_filter 回调中声明它,因为现在Rails 不知道如何正确处理此语句并且在方法之外它无法访问您的任何助手.

【讨论】:

  • 我把这段代码放进去重新加载,还是一样的错误。
  • @user3382170 你能把错误信息贴出来让我们看看它在哪里上升吗?没关系看更新。
  • 那么我应该用我的代码替换问题控制器中的更新代码,或者我应该如何处理上面的代码。
  • @user3382170 只需注释掉该行,我敢打赌它有效。不知道你想用那条线做什么,所以如果没有进一步的解释,我真的无法提供帮助,因为它目前除了破坏控制器之外没有任何影响。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多