用户已登录,但使用Rails和Devise设置为nil

时间:2018-01-07 20:47:02

标签: ruby-on-rails devise

设计(4.4.0) Rails 5.1.4 我看到这个问题很多,但找不到对我有用的答案。我正在显示我正在注册并登录,但当前用户设置为nil。我想也许是饼干,但我很难解析如何解决这个问题。一个解决方案建议添加

 Rails.application.config.session_store :cookie_store, key: '_bejoy_session', domain: :all

到配置> session_store.rb,我没有,但创建和包含。我只是不确定我在这里缺少什么。

应用程序控制器:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?
  helper_method :current_user
  helper_method :admin

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

  def authorize
    if !current_user
     flash[:alert] = "You aren't authorized to visit that page."
     redirect_to '/'
    end
  end

  def admin_authorize
    if !current_user || current_user.admin == false
     flash[:alert] = "Only administrators can visit that page."
     redirect_to new_user_session_path
    end
  end

  def admin
    current_user && current_user.admin
  end

  protected
    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :admin, :email])
    end
 end

的routes.rb

Rails.application.routes.draw do
  root :to => 'contents#index'
  resources :contents
  resources :visitors

  devise_for :users, controllers: {
    sessions: 'users/sessions',
    passwords: 'users/passwords',
    registrations: 'users/registrations'
  }
end

终端:

Started POST "/users/sign_in" for 127.0.0.1 at 2018-01-07 12:31:25 -0800
Processing by Users::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6a0yiBCkmEN0W68xN/lVZH71YhT5i5tMEkFFYqnoIvMU0NJjH3LM3hPG+8yO3D1tXskh9pSA+PRFVKDmiKzN6A==", "user"=>{"email"=>"test@tester.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
User Load (1.9ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "test@tester.com"], ["LIMIT", 1]]
(0.1ms)  BEGIN
SQL (2.0ms)  UPDATE "users" SET "current_sign_in_at" = $1, "last_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = $5  [["current_sign_in_at", "2018-01-07 20:31:26.091228"], ["last_sign_in_at", "2018-01-07 20:29:10.402839"], ["sign_in_count", 5], ["updated_at", "2018-01-07 20:31:26.092407"], ["id", 2]]
(0.4ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 152ms (ActiveRecord: 4.5ms)


Started GET "/" for 127.0.0.1 at 2018-01-07 12:31:26 -0800
Processing by ContentsController#index as HTML
User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
**current_user :nil
session id request :353a5e1d2bd295e8ac665e22e8f31314
user_signed_in? :false**
Rendering contents/index.html.erb within layouts/application
Content Load (0.2ms)  SELECT "contents".* FROM "contents"
Rendered contents/index.html.erb within layouts/application (1.5ms)
Rendered partials/_author_photo_contact_block.html.erb (0.4ms)
Completed 200 OK in 53ms (Views: 49.1ms | ActiveRecord: 0.5ms)

内容控制器:

class ContentsController < ApplicationController
   before_action :authenticate_user!
   # before_action :set_content, only: [:show, :edit, :update, :destroy]

   def index
      @contents = Content.all
      logger.debug "current_user :#{current_user.inspect}"
      logger.debug "session id request :#{request.session_options[:id]}"
      logger.debug "user_signed_in? :#{user_signed_in?}"
   end

   def show
   end

   def new
     @content = Content.new
   end

   def edit
   end

   def create
     @content = Content.new(content_params)

     respond_to do |format|
       if @content.save
         format.html { redirect_to @content, notice: 'Content was successfully created.' }
         format.json { render :show, status: :created, location: @content }
       else
         format.html { render :new }
         format.json { render json: @content.errors, status: :unprocessable_entity }
       end
     end
   end

   def update
     respond_to do |format|
       if @content.update(content_params)
         format.html { redirect_to @content, notice: 'Content was successfully updated.' }
         format.json { render :show, status: :ok, location: @content }
       else
         format.html { render :edit }
         format.json { render json: @content.errors, status: :unprocessable_entity }
       end
     end
   end

   def destroy
     @content.destroy
     respond_to do |format|
       format.html { redirect_to contents_url, notice: 'Content was successfully destroyed.' }
       format.json { head :no_content }
     end
   end

   private
     def set_content
       @content = Content.find(params[:id])
     end

     def content_params
       params.require(:content).permit(:main_image, :sidebar_image, :content, :sidebar)
     end
 end

1 个答案:

答案 0 :(得分:0)

解决方案: 应用程序控制器中的current_user方法覆盖了设计自己的current_user方法。我删除了应用程序控制器中的current_user方法,现在显示:

current_user :#<User id: 2, email: "test@tester.com", username: "", admin: false, created_at: "2018-01-07 20:09:17", updated_at: "2018-01-07 21:33:51">
session id request :all
user_signed_in? :true
current user present? :true

在这里找到答案:devise - can't display sign in or sign out in rails view