[英]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.