简体   繁体   English

登录后 ActiveAdmin 重定向不起作用

[英]ActiveAdmin redirect after login doesn't work

For some reason ActiveAdmin returns same login page after successful login:由于某种原因,ActiveAdmin 在成功登录后返回相同的登录页面:

登录表格

I thought it's something related with "cancancan" or "acts_as_tenant" so I tried to remove it but it doesn't helped.我认为它与“cancancan”或“acts_as_tenant”有关,所以我试图将其删除,但没有帮助。

I don't added anything special into /initializers/active_admin.rb and tried mostly everything..我没有在 /initializers/active_admin.rb 中添加任何特别的东西,并且几乎尝试了所有的东西..

Maybe someone had same problem before?也许有人以前有同样的问题?

ApplicationController.rb应用程序控制器

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  set_current_tenant_through_filter
  before_action :authenticate_admin_user!
  before_action :find_current_tenant, if: -> { current_admin_user }

  def find_current_tenant
    current_account = current_admin_user.business
    set_current_tenant(current_account)
  end
end

Gemfile:宝石档案:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.5'
# Use pg as the database for Active Record
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# For external requests
gem 'nokogiri'

# Admin panel
gem 'activeadmin'

# User auth
gem 'devise'

# User roles
gem 'cancan'

gem 'bootstrap', '~> 4.3.1'

# Multi tenants
gem 'acts_as_tenant'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'letter_opener'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

logs:日志:

Started POST "/admin/login" for 127.0.0.1 at 2019-07-26 17:53:46 +0300
Processing by ActiveAdmin::Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MoVGIQ6TGwK6CjqJyvyK2Igi0i389jGaX3AbSLfIkwjRqMMEENr62ka/XoKgrM6dC0K9ul5FRmKWcrrG47VCHg==", "admin_user"=>{"email"=>"admin@example.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Login"}
  [1m[36mAdminUser Load (22.5ms)[0m  [1m[34mSELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."email" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2[0m  [["email", "admin@example.com"], ["LIMIT", 1]]
  [1m[35m (0.2ms)[0m  [1m[35mBEGIN[0m
  [1m[35m (1.5ms)[0m  [1m[31mROLLBACK[0m
  Rendering /Users/alex/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activeadmin-1.2.1/app/views/active_admin/devise/sessions/new.html.erb within layouts/active_admin_logged_out
  Rendered /Users/alex/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activeadmin-1.2.1/app/views/active_admin/devise/shared/_links.erb (1.5ms)
  Rendered /Users/alex/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activeadmin-1.2.1/app/views/active_admin/devise/sessions/new.html.erb within layouts/active_admin_logged_out (60.2ms)
Completed 200 OK in 671ms (Views: 361.9ms | ActiveRecord: 24.2ms)



Some of my AdminUser's like "superadmins" doesn't have "business_id" (it's set to NULL).我的一些 AdminUser 像“超级管理员”没有“business_id”(它被设置为 NULL)。 That's was the problem, but in my case I really need to have NULL for some users.这就是问题所在,但就我而言,我确实需要为某些用户设置 NULL。

So the solution is looking like this.所以解决方案看起来像这样。

Code before:之前的代码:

class AdminUser < ApplicationRecord
  belongs_to :business
  validates :business, presence: true

  acts_as_tenant(:business)
end

And after fix:修复后:

class AdminUser < ApplicationRecord
  belongs_to :business, optional: true

  # for some reasons it's not working without "on: :create", not sure why
  validates :business, presence: true, on: :create

  acts_as_tenant(:business, optional: true)
end

Hope it will helps someone.希望它会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM