概要
deviseで認証機能を作成する
詳細
前提
Rails環境は構築済み
手順
・Gemfileに追記
gem "devise", "~> 3.2.2"
・テンプレートの生成
bundle exec rails generate devise:install
テンプレート出力時のメッセージに従い追加設定行う
・メール送信URL設定を config/environment/development.rbに追加
config.action_mailer.default_url_options = { host: 'localhost:3000' } config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => 'smtp-server', :port => 25, :user_name => 'aaaa@aaaaa' }
・config/routes.rbにroutingの追加
root to: "home#index"
・app/views/layouts/application.rbにメッセージ表示領域を追加
<p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p>
・deviceのテンプレートViewを生成
bundle exec rails g devise:views
・Userモデルの生成
bundle exec rails g devise User
rails db:migrate
・トップページの作成
rails g controller home index
・トップページの編集
<h1>Home#index</h1> <% if user_signed_in? %> <%= link_to "siginout", destroy_user_session_path, method: :delete %> <% else %> <%= link_to "siginin", new_user_session_path %> <% end %>
・動作確認のためサーバー起動
rails s