【发布时间】:2019-12-29 18:55:12
【问题描述】:
我正在使用 Ruby 2.6.5 和 Rails 5.2.2。
我对设计行为感到非常困惑。首先,我的项目在我的本地主机中工作。当我尝试将项目放入 VPS 时,错误就开始了。
具有相同 gem 版本和相同操作系统的相同代码在 VPS 和我的 localhost 两台不同机器上的工作方式不同。
VPS:
devise (4.7.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
devise-i18n (1.8.2)
devise (>= 4.6)
进入 /users/sign_in:
登录后:
还有本地主机:
devise (4.7.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
devise-i18n (1.8.2)
devise (>= 4.6)
进入 /users/sign_in:
登录后:
代码:
class Admin::HomeController < ApplicationController
layout 'admin'
before_action :authenticate_user!
before_action :set_title
def index
end
private
def set_title
@title = 'Home'
end
end
class Admin::User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable,
:recoverable, :rememberable, :validatable
end
# frozen_string_literal: true
class Admin::User::SessionsController < ::Devise::SessionsController
layout 'login'
# before_action :configure_sign_in_params, only: [:create]
# GET /resource/sign_in
# def new
# super
# end
# POST /resource/sign_in
# def create
# super
# end
# DELETE /resource/sign_out
# def destroy
# super
# end
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_in_params
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
# end
end
Rails.application.routes.draw do
devise_for :clients, controllers: {
sessions: 'clients/sessions',
confirmations: 'clients/confirmations',
omniauth: 'clients/omniauth',
registrations: 'clients/registrations',
passwords: 'clients/passwords',
unlocks: 'clients/unlocks'
}
#
# Área do cliente
#
devise_scope :client do
get 'area-do-cliente/entrar', to: 'clients/sessions#new'
get 'area-do-cliente/meus-dados', to: 'clients/registrations#edit'
end
get 'area-do-cliente/pedidos', to: 'clients/orders#index', as: 'client_orders'
devise_for :users,
class_name: "Admin::User",
controllers: {
sessions: 'admin/user/sessions',
confirmations: 'admin/user/confirmations',
omniauth: 'admin/user/omniauth',
registrations: 'admin/user/registrations',
passwords: 'admin/user/passwords',
unlocks: 'admin/user/unlocks'
}
namespace :admin do
root 'home#index'
# Some other routes ...
我解决了 VPS 真实性问题。现在它不会抛出错误,但是当我登录时它会重定向到 错误路径(不是管理员根目录),并使用 current_user nil。
Devise 真实性问题有很多问题,他们说要把它放在控制器中:
protect_from_forgery
我做到了,但现在登录后current_user 是nil。
这两种环境的不同之处在于,在 VPS 中,我使用了 Nginx 配置:
server {
listen 80;
listen [::]:80;
server_name vps12441.publiccloud.com.br;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
为什么要设计在两台不同的机器上使用完全相同的代码(和应用程序配置)获得不同的行为?
我怎样才能让它工作?
【问题讨论】:
-
欢迎来到 SO!谨慎使用强调 您可以将内容以粗体或斜体表示,但仅在真正需要时才这样做。强调用于引起对某事的额外注意。如果几乎所有事情都需要额外的关注,那么没有什么会得到额外的关注。根据经验,在用粗体或斜体写一些东西之前,问问自己:如果我只能在这篇文章中用粗体或斜体写一件事,会是这个吗?如果答案是否定的,那就不要。 meta.stackoverflow.com/q/303219/128421
标签: ruby-on-rails ruby authentication devise