Devise and Rails 6

我正在尝试使用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

controllers / application_controller.rb:

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

controllers / 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','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
achievekey 回答:Devise and Rails 6

在您的const data = {"value":"face","next":[{"value":"tace","next":[{"value":"tale","next":[{"value":"talk","next":[]}]},{"value":"tack","next":[]}]}]},{"value":"fack","next":[{"value":"tack",{"value":"falk","next":[]}]}]}]} const flatten = (obj,prev = []) => { const next = prev.concat(obj.value) return obj.next.reduce((r,e) => { if(e.next.length) r.push(...flatten(e,next)) else r.push(next.slice().concat(e.value)); return r; },[]) } const result = flatten(data); console.log(result);中,您要检查application/layout.html.erb,直到用户登录后才设置。要确定用户是否已登录,应使用current_user方法user_signed_in随附。这样您的代码就变成了

devise
本文链接:https://www.f2er.com/3149842.html

大家都在问