简体   繁体   English

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

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

I'm newbie in Ruby on Rails and currently i'm developing my first app, Devise apparently is the best gem to get this action (authentication). 我是Ruby on Rails的新手,目前正在开发我的第一个应用程序,Devise显然是执行此操作(身份验证)的最佳工具。 But i'm not understand how to work. 但我不知道如何工作。

To create a simple blog is very easy the Getting Starter Guide, i've great ideas to use RoR, but depends how to work it with Devise. 创建一个简单的博客非常容易,《入门指南》非常有用,但我会使用RoR,但要取决于如何在Devise中使用它。

I try this url: 我尝试这个网址:

Rails 3 + Devise: user_signed_in? Rails 3 + Devise:user_signed_in? for a different user in the database? 对于数据库中的其他用户? (from 2010) not work (从2010年开始)不起作用

Rails, Devise. Rails,设计。 Disallow User actions unless AdminUser (Is'nt my issue) 除非AdminUser,否则禁止用户操作 (这不是我的问题)

If you have other way to use authentication for newbies developers in this language, please tell me 如果您还有其他以这种语言为新手开发人员使用身份验证的方法,请告诉我

My Gemfile: 我的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'

In advance, thank you 提前谢谢

Can't you just use the built in Devise method authenticate_user! 您不能只使用内置的Devise方法authenticate_user! (or whatever your model name is)? (或您的型号名称是什么)? Usually this is implemented at a controller level in a before_action statement, like this: 通常,这是在控制器级别的before_action语句中实现的,如下所示:

class Controller < ApplicationController
  before_action :authenticate_user!
end

You can also limit the actions where the before action is applied. 您还可以限制应用了操作之前的操作。 That method will do exactly what you want, will first check if the user is logged in and redirect to the sign up page otherwise. 该方法将完全按照您的要求进行操作,将首先检查用户是否已登录,否则将重定向到注册页面。

before_action :authenticate_user! // Add this to your controller //将其添加到您的控制器

Assuming that your model is user. 假设您的模型是用户。 If your model is name other than user(For ex: member) user authenticate_member! 如果您的模型不是用户名(例如:成员),则用户authenticate_member!

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

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