【问题标题】:Getting 'We're sorry, something went wrong' error with Devise and Rails 6Devise 和 Rails 6 出现“我们很抱歉,出了点问题”错误
【发布时间】:2019-11-06 18:13:15
【问题描述】:

我正在尝试使用 DeviseRails_Admin gems 设置管理页面。我已经安装了devise 并创建了一个rails g devise USER。我已经创建了一个current_user 签入应用程序布局并根据 Devise 的说明和我在此处遵循的教程设置路线:https://blog.codeplace.com/building-an-admin-panel-for-your-rails-app-d672159eb26e

知道我之前有过 DIY 会话/身份验证,但已经摆脱了那个管道。我还使用了 2 个数据库:一个本地 postgresql 和一个远程 AWS RDS。

当我尝试点击home#index 时,我在浏览器中收到以下错误:

We're sorry, but something went wrong.
If you are the application owner check the logs for more information.

在终端错误是:

Started GET "/users/sign_in" for ::1 at 2019-11-06 12:47:03 -0500
   (28.6ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
Processing by Devise::SessionsController#new as HTML
  Rendering devise/sessions/new.html.erb within layouts/application
  Rendered devise/shared/_links.html.erb (Duration: 2.0ms | Allocations: 626)
  Rendered devise/sessions/new.html.erb within layouts/application (Duration: 141.2ms | Allocations: 177808)
Completed 500 Internal Server Error in 633ms (ActiveRecord: 31.4ms | Allocations: 257294)

不确定视图或控制器是否有问题,但我已经搜索了所有内容,但找不到解决方案。这是我的 Rails MVC、database.yml 和路由:

型号:

class User < ApplicationRecord
  establish_connection(:local)
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end

控制器/application_controller.rb:

class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception
    before_action :authenticate_user!
end

控制器/home_controller.rb:

class HomeController < ApplicationController
  def index
    byebug 
    if current_user
      redirect_to search_path
    end
  end
end

views/application_layout.html.erb:

<!DOCTYPE html>
<html>

<head>
  <title>Tml App</title>
  <%= csrf_meta_tags %>
  <%= csp_meta_tag %>
  <!-- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> -->

  <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> -->

  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
  <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>

  <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

  <!-- <%= image_tag 'logo.png', :style => 'width:50%; height:50%;' %> -->
</head>

<body>

  <div class="container">
    <div class="row justify-content-start">
      <div class="col-5">
        <%= image_tag 'logo-2.png', id: 'logo' %>
      </div>


      <div class="col-4 justify-content-center">
        <% if flash %>
          <% flash.each do |key, value| %>
          <div class="<%= flash_class(key) %>" id="error">
            <%= value %>
          </div>
            <% end %>
        <% else %> 
          <% flash.discard %>
        <% end %>
      </div>

      <div class="col-3">
        <% if current_user %>
        <%= link_to “Logout”, destroy_user_session_path, method: :delete %>
        <% else %>
        <%= link_to “Login”, new_user_session_path %>
        <% end %>
      </div>
    </div>

    <%= yield %>

  </div>

</body>

</html>

路线:

Rails.application.routes.draw do

  devise_for :users

  root 'home#index'

  get 'searches/index' => "searches#index"
  get 'searches/show' => "searches#show"
  get 'searches/search' => "searches#search", as: 'search' 


  get 'audits/show' => 'audits#show'     
  get 'audits/download' => 'downloads#show' 
  post 'audits/' => 'audits#create'

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end 

还有我的 database.yml 文件以防万一它可能导致问题?

default: &default 
  adapter: postgresql
  encoding: unicode
  database: tml_portal 
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>



development:
  aws:
    adapter: mysql2
    encoding: utf8
    database: requests
    username: <%= Rails.application.credentials[:aws_username] %>
    password: <%= Rails.application.credentials[:aws_password] %>
    host: <%= Rails.application.credentials[:aws_host] %>
    cast: false 
    port: 3306
  local:
    <<: *default



production:
  aws:
    adapter: mysql2
    encoding: utf8
    database: requests
    username: <%= Rails.application.credentials[:aws_username] %>
    password: <%= Rails.application.credentials[:aws_password] %>
    host: <%= Rails.application.credentials[:aws_host] %>
    cast: false 
    port: 3306
  local:
    <<: *default




test: 
  aws:
    adapter: mysql2
    encoding: utf8
    database: requests
    username: <%= Rails.application.credentials[:aws_username] %>
    password: <%= Rails.application.credentials[:aws_password] %>
    host: <%= Rails.application.credentials[:aws_host] %>
    cast: false 
    port: 3306
  local:
    <<: *default

【问题讨论】:

  • 您正在检查application/layout.html.erb 中的current_user,而您应该检查是否user_signed_in。这就是问题

标签: ruby-on-rails devise


【解决方案1】:

在您的application/layout.html.erb 中,您正在检查current_user,该current_user 在用户登录之前不会设置。要识别用户是否已登录,您应该使用user_signed_in 附带的devise 方法@ .所以你的代码变成了,

<div class="col-3">
   <% if user_signed_in? %>
        <%= link_to “Logout”, destroy_user_session_path, method: :delete %>
   <% else %>
        <%= link_to “Login”, new_user_session_path %>
   <% end %>
</div>

【讨论】:

  • 这很有意义,但即使将方法更改为 user_signed_in 后仍然没有运气
猜你喜欢
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 2012-04-19
  • 2012-06-19
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多