0
1

More than 1 year has passed since last update.

【Rails】deviseの導入とログイン機構作成時のメモ

Posted at

deviseの導入手順

Gemfileに以下を記述。deviceではなく、deviseなのでご注意を。

Gemfile
gem 'devise'

ターミナルで以下を実行し、gemのインストール。

bundle install 

以下のコマンドを実行し、設定ファイルを作成。

$ rails g devise:install

以下のコマンドを実行しモデルの作成とマイグレート

$ rails g devise user

$ rake db:migrate

ログイン機構が表示されるかどうかを確認するため、以下のようなviewをapp/views配下に作成してみる。

home/show
<h1>HOME</h1>

<% if user_signed_in? %>

  <h4> メールアドレス: <%= current_user.email %> </h4>
  <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> 
<% else %>
  <h2> 今すぐ始めよう </h2>
  <%= link_to "ログイン", new_user_session_path, class: 'post' %> 
  <%= link_to "新規登録", new_user_registration_path, class: 'post' %>
<% end %>

またルーティングも以下のように設定する。

routes.rb
Rails.application.routes.draw do
  devise_for :users
  root 'home#show'
end

最後にrails sでサーバーを立て直す。dockerで環境構築をしている場合は、コンテナをビルドし直す。

そのままローカルホストにアクセスし、内容が表示されるかを確認する。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1