`

device登录认证插件

阅读更多

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);

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics