LoginSignup
0
0

More than 3 years have passed since last update.

【Ruby on Rails】deviseの不要なルートを作らない方法

Last updated at Posted at 2020-09-29

目標

deviseで自動的に作成される不要なルートを作らない。
→新たな新規adminの作成を防ぐ。
※管理者(admin)情報はseedにて記述する場合。

開発環境

ruby 2.5.7
Rails 5.2.4.3
OS: macOS Catalina

前提

※ ▶◯◯ を選択すると、説明等が出てきますので、
  よくわからない場合の参考にしていただければと思います。

seedに記述

seedに管理者の情報を記述。

db/seeds.rb
...

Admin.create!(
  email: 'aa@aa.com',
  password: 'aaaaaa',
)
ターミナル
$ rails db:seed

これで管理者ページにログインできます。
※seedに関しては開発環境と本番環境で分ける必要があります。
詳しくはこちら

routesを編集

config/routes.rb
  devise_for :admins, :skip => [:registrations, :password],controllers: {
    sessions: 'admins/sessions',
  }

ログインページがデフォルトの状態の場合

このままではエラーが出てしまうので、下記ファイルをの該当箇所を削除。

app/views/admins/shared/_links.html.erb
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
  <%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end %>

<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
  <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<% end %>

参考

サインアップするためにDeviseルートを削除するにはどうすればよいですか?

0
0
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
0