Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails设计单表继承用户登录?始终为false,当前用户始终为nil_Ruby On Rails_Devise - Fatal编程技术网

Ruby on rails Rails设计单表继承用户登录?始终为false,当前用户始终为nil

Ruby on rails Rails设计单表继承用户登录?始终为false,当前用户始终为nil,ruby-on-rails,devise,Ruby On Rails,Devise,我试着做这个链接 之后,我使用/admin/sign_登录,在控制器中创建断点。我发现当前用户总是返回nil,用户是否已登录?这总是错误的 有人能帮我吗 # model class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :d

我试着做这个链接

之后,我使用/admin/sign_登录,在控制器中创建断点。我发现当前用户总是返回nil,用户是否已登录?这总是错误的

有人能帮我吗

# model
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  #attr_accessible :name, :email, :password, :password_confirmation
end

class Customer < User
end

class Restaurant < User
end

class Admin < Customer
end

class Staff < Customer
end

# application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  # Devise helper configuration
  helper_method :current_admin, :current_restaurant, :current_staff,
    :require_admin!, :require_restaurant!, :require_staff!

  include DeviseModule
end

# concern/devise_module.rb
module DeviseModule
  def account_url
    return new_user_session_url unless user_signed_in?
    case current_user.class.name
    when "Admin"
      admins_root_url
    when "Staff"
      staffs_root_url
    when "Restaurant"
      restaurants_root_url
    else
      root_url
    end if user_signed_in?
  end

  def after_sign_in_path_for(resource)
    stored_location_for(resource) || account_url
  end

  private

  def current_admin
    @current_admin ||= current_user if user_signed_in? and current_user.class.name == "Admin"
  end

  def current_staff
    @current_staff ||= current_user if user_signed_in? and current_user.class.name == "Staff"
  end

  def current_restaurant
    @current_restaurant ||= current_user if user_signed_in? and current_user.class.name == "Restaurant"
  end

  def admin_logged_in?
    @admin_logged_in ||= user_signed_in? and current_admin
  end

  def staff_logged_in?
    @Buyer_logged_in ||= user_signed_in? and current_staff
  end

  def restaurant_logged_in?
    @seller_logged_in ||= user_signed_in? and current_restaurant
  end

  def require_admin
    require_user_type(:admin)
  end

  def require_staff
    require_user_type(:staff)
  end

  def require_restaurant
    require_user_type(:restaurant)
  end

  def require_user_type(user_type)
    if (user_type == :admin and !admin_logged_in?) or
      (user_type == :staff and !staff_logged_in?) or
      (user_type == :restaurant and !restaurant_logged_in?)
      redirect_to root_path, status: 301, notice: "You must be logged in a#{'n' if user_type == :admin} #{user_type} to access this content"
      return false
    end
  end
end

# router.rb
Rails.application.routes.draw do

  devise_for :users
  devise_for :admins, :skip => [:registrations]
  devise_for :restaurants
  devise_for :staffs

  scope :module => :home do

    scope :orders do
      get "/", :to => "order#index"
      get "/checkout", :to => "order#checkout"
      post "/checkout", :to => "order#create_an_order", :as => "order_checkout"
    end

    root :to => "home#index"

  end

  namespace :admins do
    resources :restaurant 
    root :to => "restaurant#index"
  end

  namespace :staffs do
    root :to => "staff#index"
  end

  namespace :restaurants do
    root :to => "retaurant#index"
  end
end
#模型
类用户[:注册]
设计餐厅
为员工设计
作用域:模块=>:home do
经营范围:订单办
获取“/”,:to=>“订单#索引”
获取“/结帐”,到=>“订单#结帐”
post“/checkout”,:to=>“订单#创建订单”,:as=>“订单#结帐”
结束
root:to=>“主页#索引”
结束
名称空间:管理员做什么
资源:餐厅
root:to=>“餐厅索引”
结束
名称空间:员工可以
root:to=>“员工#索引”
结束
名称空间:餐厅
root:to=>“保留索引”
结束
结束

因此我可以假设您使用的代码与博客中的代码完全相似。提供rake routes的o/p,并粘贴routes文件以回答此问题better@GhostRider:我更新了上面的代码。你能帮我吗?非常感谢。