PJCHENder 未整理筆記

[Gem] Devise

2017-12-25

[Gem] Devise

@(Ruby on Rails)[rails, gem]

Install

1
2
3
# Gemfile
# Flexible authentication solution for Rails with Warden
gem 'devise', '~> 4.3'

Usage

1
2
3
4
5
6
# in controller
class Admin::BaseController < ApplicationController
before_action :authenticate_user!

layout 'admin/layouts/application'
end
1
2
3
4
<!-- in view -->
<%= link_to destroy_user_session_path, method: :delete, class: 'nav-link' do %>
<i class="fas fa-sign-out-alt"></i><span class="ml-1">登出</span>
<% end %>

設定可接收的註冊資料(參數)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
# Add additional parameters for Devise Model
before_action :configure_permitted_parameters, if: :devise_controller?

protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: %i(name title birthday sex phone timezone))
devise_parameter_sanitizer.permit(:account_update, keys: %i(name title birthday sex phone timezone))
end

def redirect_to_unauthorized
redirect_to unauthorized_path
return
end
end

Example

掃描二維條碼,分享此文章