Authentication: 使用 Devise
devise是一套使用者認證(Authentication)套件,是Rails社群中最廣為使用的一套。
編輯 Gemfile 加上
gem 'devise'
輸入bundle install安裝此套件
輸入rails g devise:install產生devise設定檔
編輯 config/environments/development.rb 和 production.rb 加入寄信時預設的網站網址:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
確認 app/views/layouts/application.html.erb layout 中可以顯示 flash 訊息,例如
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
確認 routes.rb 中有設定網站首頁位置,例如
root :to => "welcome#index"
輸入rails g devise user產生 User model 及 Migration
如果需要E-mail驗證功能,可以編輯app/models/user.rb和migration將confirmable功能打開
輸入rails generate devise:views產生樣板,這會包括有註冊、登入、忘記密碼、Email等等頁面,放在app/views/devise目錄下。
輸入bin/rake db:migrate建立資料表
用法
在需要登入的 controller 加上before_action :authenticate_user!
可以在 Layout 中加上登入登出選單
<% if current_user %>
<%= link_to('登出', destroy_user_session_path, :method => :delete) %> |
<%= link_to('修改密碼', edit_registration_path(:user)) %>
<% else %>
<%= link_to('註冊', new_registration_path(:user)) %> |
<%= link_to('登入', new_session_path(:user)) %>
<% end %>
加上自訂欄位
Devise預設沒有產生出first_name、last_name等等欄位,我們可以加一些欄位到User Model:
rails g migration add_username_to_users,加上
add_column :users, :username, :string
rake db:migrate 新增這個欄位
編輯application_controller.rb補上configure_permitted_parameters方法:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
# ...
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
devise_parameter_sanitizer.for(:account_update) << :username
end
end
編輯views/devise/registrations/edit.html.erb和views/devise/registrations/new.html.erb,加上username欄位
<div><%= f.label :username %><br />
<%= f.text_field :username %></div>
进入项目
1、添加gem
gem 'devise'
2、更新本地gem
bundle install
3、创建页面
rails g controller home index
4、初始化devise
rails g devise:install
会有如下提示:
1. Setup default url options for your specific environment. Here is an
example of development environment:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
This is a required Rails configuration. In production it must be 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:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
4. If you are deploying Rails 3.1 on Heroku, 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、与模型User关联
rails g devise User
这条命令会生成一个user.rb model文件,一个migration文件和一个devise_for的路由
我们可以看到User model和普通的ActiveRecord的区别并不大,主要的差别是调用了devise方法,当然这也是配置的关键。Devise方法有很多的参数用来标识是否使用对应的功能模块
Devise参数?(还不了解)
6.执行迁移任务
rake db:migrate
查看在router.rb文件中的devise_for都产生了什么路由.可以通过rake routes查看
产生了如下路由:登录,登出,重置密码,注册,和修改。如果我们需要,所有这些路由都是可以配置的
7、修改默认访问url
打开routes.rb,加入:root :to => "home#index"
8、修改index.html.erb代码
<% if user_signed_in? -%> <!-- Provided by devise -->
<div style="float:right">
<%= current_user.email %> |
<%= link_to '用户信息', edit_user_registration_path %> |
<%= link_to '退出登录', destroy_user_session_path, :method => :delete %> |
</div>
<% end -%>
<% unless user_signed_in? -%>
<div style="float:right">
<%= link_to '注册', new_user_registration_path %> |
<%= link_to '登录', new_user_session_path %>
</div>
<% end -%>
9、启动服务
rails s
10、测试
localhost:3000/home/index
1.下拉框中存放的是图片的路径,触发下拉框的change事件,得到url,然后显示在页面上。例如:<IMG id="avatarImg"> 然后$("#avatarImg").attr("src",url);
相关推荐
CANPro1.5 DeviceNet插件
DeviceNet从站设备开发的源码,采用了microchip的MCU,对于做DeviceNet从站设备的开发人员来说,有很大参考意义
wd ses device usb device 西部数据 移动硬盘 驱动 wd ses device usb device 西部数据 移动硬盘 驱动 wd ses device usb device 西部数据 移动硬盘 驱动
最新 wd ses device usb device 西部数据移动硬盘 驱动下载。 支持WIN2003、XP等系统。
HCNA-HNTD (Huawei Certified Network Associate - Huawei Network Technology and Device)认证考试题库
获得google认证所需GSFID的deviceID.apk
屏幕适配插件
基于STM32开发的DEVICENET的不完全原代码,由于版权有限,里边有几处需要修改后才能完全符合标准。
iOS 11.3 DeviceSupport 真机支持包,解压放到指定位置,位置里面有
Springer.Smart Device to Smart Device Communication.2014
获取deviceID 的方法,项目中已使用
一、准备工作 配置DeviceAdmin 配置DeviceAdmin可参考 android权限级别探索(二),设备管理器开启和使用及常见api,创建DeviceReceiver继承DeviceAdminReceiver,并在AndroidManifest中注册,完成两步即可。无需将...
devicenet相关资源,包含: 中文协议,相关产品介绍,参考资料,环境搭建,相关代码等。 部分文档名称如下: CAN-bus-shiyanshi.pdf Device Net总线知识汇总贴.doc DeviceNet 及ODVA概要.doc Device...
vite-plugin-multi-device :rocket: 一个代码库,多个输出适用于多种设备的Vite插件使用为多个设备构建应用程序并通过消除无效代码。安装该插件仅适用于Vite> = 2 $ npm install -D vite-plugin-multi-device// Or$ ...
新更新的12.0缺少SDK可用 command + shift + G 前往文件夹 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 将更新包解压进去即可
Xcode DeviceSupport ios 10.3 Xcode DeviceSupport ios 10.3Xcode DeviceSupport ios 10.3
Simatic device drive
/ # usb 1-1: new full speed USB device using s3c2410-ohci and address 2 usb 1-1: device descriptor read/64, error -62 usb 1-1: device descriptor read/64, error -62 usb 1-1: new full speed USB device ...
D2D 外文文献 Smart-device-to-smart-device communications D2D 外文文献 Smart-device-to-smart-device communications
iOS 11 device support 大家可以把文件复制到Xcode目录下使用