繁体   English   中英

检查用户是否登录,否则重定向到登录页面

[英]Check if user is logged in, else redirect to login page

我是Ruby on Rails的新手,目前正在开发我的第一个应用程序,Devise显然是执行此操作(身份验证)的最佳工具。 但我不知道如何工作。

创建一个简单的博客非常容易,《入门指南》非常有用,但我会使用RoR,但要取决于如何在Devise中使用它。

我尝试这个网址:

Rails 3 + Devise:user_signed_in? 对于数据库中的其他用户? (从2010年开始)不起作用

Rails,设计。 除非AdminUser,否则禁止用户操作 (这不是我的问题)

如果您还有其他以这种语言为新手开发人员使用身份验证的方法,请告诉我

我的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.0.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# 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', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

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

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
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'
  # 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'
end

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

# Devise for login
gem 'devise'

# Bootstrap
gem 'bootstrap-sass', '~> 3.3.6'

# Google Web Fonts
gem 'google-webfonts-rails'

提前谢谢

您不能只使用内置的Devise方法authenticate_user! (或您的型号名称是什么)? 通常,这是在控制器级别的before_action语句中实现的,如下所示:

class Controller < ApplicationController
  before_action :authenticate_user!
end

您还可以限制应用了操作之前的操作。 该方法将完全按照您的要求进行操作,将首先检查用户是否已登录,否则将重定向到注册页面。

before_action :authenticate_user! //将其添加到您的控制器

假设您的模型是用户。 如果您的模型不是用户名(例如:成员),则用户authenticate_member!

暂无
暂无

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

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