5
0

More than 1 year has passed since last update.

シンプルな管理者機能【RubyOnRails】

Last updated at Posted at 2022-11-29

目標

事前に設定した管理者用パスワードを打ち込んだ場合のみ、管理者用メニューを見れるようにする。

環境

  • Ruby 3.0.4
  • Rails 6.1.7

方針

管理者用ログインボタンから管理者用パスワード入力画面(admin_session)に遷移させ、入力した内容をadmin_authenticationに送り、事前に.env上に用意しておいたパスワードと一致したときのみ、メニュー画面(admin_index)を表示(レンダー)させる。一致しない場合は管理者用パスワード入力画面(admin_session)を表示(リダイレクト)させる。

完成形

コントローラー(トップページを扱うもの:今回はhello_controller.rb)
hello_controller.rb
#前略
    
    def admin_session
    end 
    
    def admin_authentication
    if params[:input_password] == ENV['ADMIN_PASSWORD']
      render "admin_index"
    else
      flash[:fail]="パスワードが違います"
      redirect_to hello_admin_session_path
    end
    end
    
    def admin_index
    end

#後略
ルーティング
routes.rb
#前略
  get 'hello/admin_authentication'=> 'hello#admin_session'
  post 'hello/admin_authentication'=> 'hello#admin_authentication'
  get 'hello/admin_session' => 'hello#admin_session'
  get 'hello/admin_index' => 'hello#admin_index'
#後略
ビューファイル
admin_session.html.erb
<p>パスワードを入力してください</p>
<%= form_tag hello_admin_authentication_path, method: :post do %>
  <%= password_field_tag 'input_password' %>
  <%= button_tag do %>
    <span>確認</span>
  <% end %>
<% end %>

<%= flash[:fail] %>
admin_index.html.erb
<h1>ここは管理者用メニューです</h1>
envファイル
.env
ADMIN_PASSWORD = 1111 

手順

1.gem "dotenv-rails"の導入

dotenv-railsとは、環境変数を管理する事が出来るgemです。
まずは、dotenv-railsをインストールする為に、Gemfileに下記を追加してbundle installします。

Gemfile
gem 'dotenv-rails'
コマンドプロンプト/ターミナル
bundle install

次に、アプリケーションのルート直下に.envファイルを作成します。
S__74891288.jpg

最後に、作成した.envファイルにパスワードを記述します。
今回は”1111”としておきますが、自由に変更してください。

.env
ADMIN_PASSWORD = 1111 

2.ビューファイルの記述

今回は入力した内容をDBに保存したいわけではないので、form_tagで、methodにpostを指定します。
また、パスワードが一致しなかったときのためにflashメッセージを用意しておきます。

admin_session.html.erb
<p>パスワードを入力してください</p>
<%= form_tag hello_admin_authentication_path, method: :post do %>
  <%= password_field_tag 'input_password' %>
  <%= button_tag do %>
    <span>確認</span>
  <% end %>
<% end %>

<%= flash[:fail] %>
admin_index.html.erb
<h1>ここは管理者用メニューです</h1>

3.ルーティングとコントローラーの設定

ルーティング

hello/admin_authenticationに関しては、post(つまりform_tagに入力)があったときにはパスワードの認証をするためにhello#admin_authenticationアクションへ、またリロード等でgetがあったときにはhello#admin_sessionアクションへ送り、admin_session.html.erbを表示させます。

routes.rb
#前略
  get 'hello/admin_authentication'=> 'hello#admin_session'
  post 'hello/admin_authentication'=> 'hello#admin_authentication'
  get 'hello/admin_session' => 'hello#admin_session'
#後略
コントローラー(トップページを扱うもの:今回はhello_controller.rb)

admin_session.html.erbでの入力をparams[:input_password]で受け取り、ENV['ADMIN_PASSWORD']で.envファイルから持ってきたパスワードと比較し、if文で処理します。
一致した場合はadmin_index.html.erbへ、一致しなかった場合はadmin_session.html.erbへリダイレクトし、flashを使ってメッセージを呼び出します。

hello_controller.rb
#前略
    
    def admin_session
    end 
    
    def admin_authentication
    if params[:input_password] == ENV['ADMIN_PASSWORD']
      redirect_to hello_admin_index_path
    else
      flash[:fail]="パスワードが違います"
      redirect_to hello_admin_session_path
    end
    end
    
    def admin_index
    end

#後略

参考

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