【问题标题】:How to create `authenticate_user' method without devise in ror如何在不设计 ror 的情况下创建“authenticate_user”方法
【发布时间】:2014-01-08 21:11:38
【问题描述】:

我是 Ruby on Rails 的新手,我使用的是 Ruby 1.9.3 版和 Rails 4.0.2 版。

我的查询是:-

如何在没有设计的情况下在 Ruby on Rails 中创建 `authenticate_user' 方法。

在我的路线下方

get "admin/users/sign_in" => "admin/users#sign_in"

在我的应用程序控制器下:-

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception
  rescue_from CanCan::AccessDenied do |exception|
    flash[:alert] = "Access denied. You are not authorized to access the requested page."
    redirect_to root_path and return
  end


  helper_method :current_user
  before_filter :authenticate_user, :current_user

  def current_user
    # Note: we want to use "find_by_id" because it's OK to return a nil.
    # If we were to use User.find, it would throw an exception if the user can't be found.
    @current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
    @current_user ||= User.find_by_authentication_token(cookies[:auth_token]) if   cookies[:auth_token] && @current_user.nil?
    @current_user
  end

  def authenticate_user
    if @current_user.nil?
      flash[:error] = 'You must be signed in to view that page.'
      redirect_to :admin_users_sign_in

    end
  end


  protected

  #derive the model name from the controller. egs UsersController will return User
  def self.permission
    return name = self.name.gsub('Controller','').singularize.split('::').last.constantize.name rescue nil
  end

  def current_ability
    @current_ability ||= Ability.new(current_user)
  end

  #load the permissions for the current user so that UI can be manipulated
  def load_permissions
    @current_permissions = current_user.role.permissions.collect{|i| [i.subject_class, i.action]}
  end

end

下面的代码使用我的控制器

before_filter :authenticate_user!

我的 authenticate_user 方法没有正确重定向

redirect_to :admin_users_sign_in

admin_users_sign_in路径定义见顶部

每次在浏览器上显示的代码上方“页面未正确重定向”

请帮忙

【问题讨论】:

  • 也许 railscasts 250 和 270 可以帮助你

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


【解决方案1】:

我怀疑问题是由于这一行:

  redirect_to :admin_users_sign_in

您需要将actioncontroller 或路径的友好名称传递给redirect_to

将您的路线更改为类似

get "admin/users/sign_in" => "admin/users#sign_in", :as => :admin_user_signin

然后你可以做类似的事情

redirect_to admin_user_signin_path

【讨论】:

  • 我使用了你的代码。我正在使用 redirect_to admin_user_signin_path 此代码“页面未正确重定向”显示错误没有浏览器。如果我使用 redirect_to :admin_user_signin_path 此代码“#<:userscontroller:0x4edc6f8> 的未定义方法 `admin_user_signin_path_url'”在代码中显示错误。
【解决方案2】:

这看起来是一个无限循环。

您在 ApplicationController 级别定义了 authenticate_user。因此,当访问者访问页面 'foo' 时,他会被此方法拒绝,因为 current_user 为 nil。然后他被重定向到管理员登录页面,但是那个页面也有这个 before_filter,所以他再次被重定向到同一个页面并且永远不会结束。

要解决此问题,请将此类过滤器移至需要保护的特定控制器。并且不要在登录/注册页面中设置。

旁注:

您已经使用了 CanCan,它也具有“读取”权限。没有必要再次使用 authenticate_user 来实现相同的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多