【发布时间】: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