跳至主要内容

[gem] Devise

Install

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

Usage

# in controller
class Admin::BaseController < ApplicationController
before_action :authenticate_user!

layout 'admin/layouts/application'
end
<!-- 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 %>

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

# 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