0

0

揭示设计的深度,第 1 部分

王林

王林

发布时间:2023-08-26 22:25:16

|

576人浏览过

|

来源于php中文网

原创

揭示设计的深度,第 1 部分

在我之前的一些关于 Rails 中图像上传的文章中,我提到了 Devise,但没有深入探讨它。在本教程中,我将教您有关 Devise 的知识。

准备好了吗?让我们开始吧!

设计简介和模块

Devise 是与 Warden 一起构建的 Rails 身份验证解决方案,由 Plataformatec 的优秀人员提供。 Devise提供了不同的模块:

  • 数据库可验证:这会将密码加密并存储到数据库中,以在登录时验证用户的真实性。
  • Omniauthable:这将 OmniAuth 支持附加到 Devise。您的应用程序的用户将能够使用 Facebook、Twitter 和 Google 等帐户登录。

  • 可确认:这可以发送包含有助于验证帐户的说明的电子邮件。

  • 可恢复:当用户忘记密码并需要恢复密码时,此模块会有所帮助。这样,用户就可以重置密码。

  • 可注册:处理用户的注册。它还允许用户编辑和删除他们的帐户。

  • Rememberable:此模块使您的应用程序可以通过存储 cookie 来记住登录的用户。

  • 可跟踪:此模块有助于跟踪登录计数、时间戳和 IP 地址。

  • 可超时:此模块负责使一段时间内不活动的会话过期。

  • 可验证:使用此模块,可以验证电子邮件和密码。

  • 可锁定:这提供了额外的安全层 - 激活后,帐户可以在登录尝试失败一定次数后被锁定。

设备集成

出于本教程的目的,我们将生成一个 Rails 应用程序,用于检查 Devise 的工作情况。我们继续吧!

rails 新设计应用 -T

-T 标志告诉 Rails 在没有默认测试套件的情况下生成应用程序。导航到您的应用程序目录并将以下 gems 放入您的 Gemfile

#Gemfile

gem 'devise', '~> 4.1'
gem 'bootstrap-sass', '~> 3.3'

现在安装您刚刚添加的 Devise 和 Bootstrap gem。

捆绑安装

将您的 app/assets/stylesheets/application.css 文件重命名为 app/assets/stylesheets/application.scss 并在其中添加以下行:

#app/assets/stylesheets/application.scss

@import "bootstrap-sprockets";
@import "bootstrap";

打开 app/assets/javascripts/application.js 文件并需要 bootstrap-sprockets。我的看起来像这样:

#app/assets/javascripts/application.js

//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require turbolinks
//= require_tree .

接下来,您需要运行 Rails 命令来安装 Devise 的配置文件。您可以通过运行以下命令来执行此操作:

rails 生成设备:install

该命令在您的终端上生成以下内容。您应该阅读它以了解发生了什么。

create  config/initializers/devise.rb
     create  config/locales/devise.en.yml
===============================================================================

Some setup you must do manually if you haven't yet:

 1. Ensure you have defined default url options in your environments files. Here
    is an example of default_url_options appropriate for a development environment
    in config/environments/development.rb:

      config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

    In production, :host should be set to the actual host of your application.

 2. Ensure you have defined root_url to *something* in your config/routes.rb.
    For example:

      root to: "home#index"

 3. Ensure you have flash messages in app/views/layouts/application.html.erb.
    For example:

      

<%= notice %>

<%= alert %>

4. If you are deploying on Heroku with Rails 3.2 only, you may want to set: config.assets.initialize_on_precompile = false On config/application.rb forcing your application to not access the DB or load models when precompiling your assets. 5. You can copy Devise views (for customization) to your app by running: rails g devise:views ===============================================================================

该命令还会生成两个文件,您可以在 config 目录中找到它们。它还为我们提供了一些关于我们应该做什么的指示。

导航到您的应用程序布局,app/views/layouts/application.html.erb,并使其如下所示:

#app/views/layouts/application.html.erb




  DeviseApp
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>


  

<%= notice %>

<%= alert %>

<%= yield %>

您需要为您的开发环境定义默认 URL 选项。在 config/environments/development.rb 中添加以下代码。

#config/environments/development.rb

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

现在您需要为 Devise 创建一个 User 模型。您可以使用终端来执行此操作。

rails 生成设备 User

这将在您的 app/models 目录中生成一个 user.rb 文件。生成的文件将如下所示:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

你可以看到它包含了我上面提到的默认模块。您运行的命令还通过添加 devise 的路由来修改您的 config/routes.rb 文件。你应该检查一下。

此时,您需要迁移数据库。您可以通过运行以下命令来执行此操作:

MvMmall 网店系统
MvMmall 网店系统

免费的开源程序长期以来,为中国的网上交易提供免费开源的网上商店系统一直是我们的初衷和努力奋斗的目标,希望大家一起把MvMmall网上商店系统的免费开源进行到底。2高效的执行效率由资深的开发团队设计,从系统架构,数据库优化,配以通过W3C验证的面页模板,全面提升页面显示速度和提高程序负载能力。3灵活的模板系统MvMmall网店系统程序代码与网页界面分离,灵活的模板方案,完全自定义模板,官方提供免费模

下载

rake 数据库:migrate

使用设备进行身份验证

现在您需要创建一个 PagesController 并在其周围封装 Devise 身份验证 - 这将防止未经授权的人员查看该页面。

rails 生成控制器页面索引

打开您的路由文件并设置应用程序的根目录。

#config/routes.rb

Rails.application.routes.draw do
  devise_for :users
  root to: "pages#index"
end

打开 PagesController 并为 indexnew 页面添加身份验证。

#app/controllers/pages_controller.rb

class PagesController < ApplicationController
  before_action :authenticate_user!, only: [:index, :new]

  def index
  end

  def new
  end
end

代码显示 indexnew 页面仅可供注册用户访问。打开终端并启动 rails 服务器。将浏览器指向 http://localhost:3000,您将自动重定向到 Devise 登录页面。

不使用电子邮件登录

登录 Devise 的默认方式涉及使用电子邮件地址和密码。如果您想让用户能够使用其唯一的用户名登录怎么办?如果那是你想要的,那是可能的。让我们看看如何。

运行命令:

rails 生成迁移 AddUsernameToUSers 用户名:string

这将在 users 表中为 username 添加一个新列。迁移您的数据库。

rake 数据库:migrate

您需要在视图中添加一个字段,用户可以在其中输入他们的用户名。当您转到 app/views 目录时,您将找不到任何呈现 Devise 视图的文件。这是因为 Devise 从其 gemset 加载视图。要自定义它,您必须生成视图的副本。下面的命令发挥了魔力。

rails 生成设备:views

这将在您的 app/views 目录中生成一些文件夹和文件。

您将需要编辑用于登录、注册和更新用户信息的页面。只需将下面的代码块粘贴到各自的文件中即可。

注册

#app/views/devise/registrations/new.html.erb

Sign up

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %>
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
<%= f.label :username %> <%= f.text_field :username, class: "form-control" %>
<%= f.label :password %> <% if @minimum_password_length %> (<%= @minimum_password_length %> characters minimum) <% end %>
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
<%= f.submit "Sign up", class: "btn btn-primary" %>
<% end %> <%= render "devise/shared/links" %>

编辑

#app/views/devise/registrations/edit.html.erb

Edit <%= resource_name.to_s.humanize %>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= devise_error_messages! %>
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
<% end %>
<%= f.label :username %> <%= f.text_field :username, class: "form-control" %>
<%= f.label :password %> (leave blank if you don't want to change it)
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
<%= f.label :current_password %> (we need your current password to confirm your changes)
<%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
<%= f.submit "Update", class: "btn btn-primary" %>
<% end %>

Cancel my account

Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %>

<%= link_to "Back", :back %>

登录

#app/views/devise/sessions/new.html.erb

Log in

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<%= f.label :username %>
<%= f.text_field :username, autofocus: true, class: "form-control" %>
<%= f.label :password %>
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
<% if devise_mapping.rememberable? -%>
<%= f.check_box :remember_me %> <%= f.label :remember_me %>
<% end -%>
<%= f.submit "Log in", class: "btn btn-primary" %>
<% end %> <%= render "devise/shared/links" %>

使用文本编辑器导航至 app/controllers/application_controller.rb。您需要修改它以允许使用用户名。将其修改为如下所示:

#app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
    devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
    devise_parameter_sanitizer.permit :account_update, keys: added_attrs
  end
end

现在用户可以使用他/她的用户名登录。此时,您的申请有一些不正确的地方。当用户登录时,无法退出。这不会带来良好的用户体验。我将向您展示如何解决这个问题。

在终端中,在 app/views 文件夹中创建一个名为 shared 的新目录。

mkdir app/views/shared
touch app/views/shared/_navigation.html.erb

您上面创建的文件是用于写入导航栏代码的部分文件。添加以下代码。

#app/views/shared/_navigation.html.erb


现在您需要在应用程序布局中呈现导航栏。打开 app/views/layouts/application.html.erb 并放入代码以呈现导航栏。

#app/views/layouts/application.html.erb

...
<%= render "shared/navigation" %>

<%= notice %>

<%= alert %>

...

结论

在本部分中,您学习了如何安装 Devise 并向页面添加身份验证。我还提到了一部分。我将在单独的教程中介绍这一点。

在下一部分中,我们将介绍一些比这更高级的领域。我希望这值得您花时间!

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

0

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

0

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

2

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

6

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

18

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

3

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

2

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

1

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号