railsにdeviseを導入しました。
導入手順を残しておこうと思います。
環境
Rails | 5.2.0 |
---|---|
ruby | 2.5.1 |
mysql | 5.7.20 |
インストール
Gemfile
gem 'devise'
$ bundle install
$ bin/rails g devise:install
create config/initializers/devise.rb
create config/locales/devise.en.yml
===============================================================================
Some setup you must do manually if you haven't yet:
1. Ensure you have defined default url options in your environments files. Here
is an example of default_url_options appropriate for a development environment
in config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
In production, :host should be set to 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. You can copy Devise views (for customization) to your app by running:
rails g devise:views
===============================================================================
development.rbの編集
1の通りにconfig/environments/development.rbに以下を追加
config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
rootページの作成&設定
2についてはrootのページ(http://localhost:3000) を作成します。Ruby on Rails チュートリアルの
「第3章 ほぼ静的なページの作成」に習って作成したいと思います。
コンソール上では例としてroot to: "home#index"とありますが、
今回はroot to: "static_pages#home"にしたいと思います。
$ bin/rails g controller StaticPages home help
create app/controllers/static_pages_controller.rb
route get 'static_pages/home'
get 'static_pages/help'
invoke erb
create app/views/static_pages
create app/views/static_pages/home.html.erb
create app/views/static_pages/help.html.erb
invoke rspec
create spec/controllers/static_pages_controller_spec.rb
invoke helper
create app/helpers/static_pages_helper.rb
invoke rspec
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.coffee
invoke scss
create app/assets/stylesheets/static_pages.scss
config/routes.rb
root to: 'static_pages#home'
フラッシュメッセージの表示
3についてはフラッシュメッセージが表示されるようにapplication.html.erbを編集します。
apps/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Blog1</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
</html>
DeviseのViewファイルの作成
4はDeviseのviewファイルを作成します。
$ bin/rails g devise:views
invoke Devise::Generators::SharedViewsGenerator
create app/views/devise/shared
create app/views/devise/shared/_links.html.erb
invoke form_for
create app/views/devise/confirmations
create app/views/devise/confirmations/new.html.erb
create app/views/devise/passwords
create app/views/devise/passwords/edit.html.erb
create app/views/devise/passwords/new.html.erb
create app/views/devise/registrations
create app/views/devise/registrations/edit.html.erb
create app/views/devise/registrations/new.html.erb
create app/views/devise/sessions
create app/views/devise/sessions/new.html.erb
create app/views/devise/unlocks
create app/views/devise/unlocks/new.html.erb
invoke erb
create app/views/devise/mailer
create app/views/devise/mailer/confirmation_instructions.html.erb
create app/views/devise/mailer/email_changed.html.erb
create app/views/devise/mailer/password_change.html.erb
create app/views/devise/mailer/reset_password_instructions.html.erb
create app/views/devise/mailer/unlock_instructions.html.erb
管理者モデル(AdminUser)の作成
$ bin/rails g devise AdminUser
invoke active_record
create db/migrate/20180603052357_devise_create_admin_users.rb
create app/models/admin_user.rb
invoke rspec
create spec/models/admin_user_spec.rb
invoke factory_bot
create spec/factories/admin_users.rb
insert app/models/admin_user.rb
route devise_for :admin_users
$ bin/rails db:migrate
ログイン/ログアウト等のナビゲーションを追加
apps/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Blog1</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<header>
<nav>
<% if admin_user_signed_in? %>
<%= link_to 'プロフィール', edit_admin_user_registration_path %>
<%= link_to 'ログアウト', destroy_admin_user_session_path, method: :delete %>
<% else %>
<%= link_to 'サインアップ', new_admin_user_registration_path %>
<%= link_to 'ログイン', new_admin_user_session_path %>
<% end %>
</nav>
</header>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
</html>
確認