質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

439閲覧

deviseでえbootstrapのflashを出したいです

RThe

総合スコア25

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/01/10 10:01

現在、deviseを導入して管理者と一般の2つのログインフォームを実装しております。
一般ログインにて管理者でログインすると弾かれて、トップに遷移する内容を作成しているのですが、弾かれるのですが、flashが出ない状態です。
ログイン画面でのエラー時は出るのでflashの導入はできているとは思いますが、
他の設定などがいるのでしょうか。

お忙しい中恐れ入りますが、解決策をご教授いただけると幸いです。

■状況

↓ログインのミスはエラーが出る
イメージ説明

■実現したい内容

・上記のフォームにadminがtrueのusre入る
・トップページに遷移←ok
・ flash[:alert] = 'こちらからはログインできません'を出したい←出ない

*ログインフォーム*

erb

1<div class="box"> 2 <div class="box-inner"> 3 <h3>ご契約者様ログイン</h3> 4 5 <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> 6 <div class="box-email"> 7 <%= f.label :email %><br /> 8 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 9 </div> 10 <div class="box-password"> 11 <%= f.label :password %> 12 <%= f.password_field :password, autocomplete: "current-password" %> 13 <div class="column-right"> 14 <%= link_to "パスワードをお忘れですか?", "#" %> 15 </div> 16 </div> 17 <% if devise_mapping.rememberable? %> 18 <div class="field"> 19 <%= f.check_box :remember_me %> 20 <%= f.label :remember_me %> 21 </div> 22 <% end %> 23 <div class="box-login"> 24 <%= f.submit "ログイン" %> 25 </div> 26 <% end %> 27 </div> 28</div> 29<%= render "devise/shared/links" %> 30<div id="new" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true"></div> 31

*application.html.erb*

erb

1<!DOCTYPE html> 2<html> 3 <head> 4 <title>AccepteApp</title> 5 <%= csrf_meta_tags %> 6 <%= csp_meta_tag %> 7 8 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 9 <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> 10 </head> 11 12 <body> 13 <div class="container"> 14 <%= render 'layouts/flashes' %>←flashファイルをレンダリング 15 <% 16 17 <%= yield %> 18 <%= debug(params) if Rails.env.development? %> 19 </div> 20 </body> 21</html> 22

*user.rb*

rb

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :rememberable 5 # 登録される時点でアドレスを小文字にする。 6 before_save { self.email = email.downcase } 7 8 validates :name, presence: true, length: { maximum: 20 } 9 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 10 validates :email, presence: true, length: { maximum: 100 },format: { with: VALID_EMAIL_REGEX },uniqueness: true 11 validates :contact, presence: true, length: { minimum: 1, maximum: 31 } 12 validates :password, presence: true, length: { minimum: 6 } 13end 14

*application_controller*

rb

1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 4 before_action :sign_out_user, if: :user_signed_in? 5 # ログイン後のパス 6 def after_sign_in_path_for(resource) 7 8 redirect_to root_path 9 flash[:alert] = 'こちらからはログインできません' **←これが出ない** 10 end 11 # adminはログアウトさせる 12 def sign_out_user 13 sign_out_and_redirect(current_user) if current_user.admin 14 end 15 16end 17

*devise_helper*

rb

1module DeviseHelper 2 def bootstrap_alert(key) 3 case key 4 when "alert" 5 "danger" 6 when "notice" 7 "success" 8 when "error" 9 "danger" 10 end 11 end 12end

宜しくお願い致します

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hatsu

2021/01/10 10:32

どこまで関係あるかわからないですが、 redirect_to root_path flash[:alert] = 'こちらからはログインできません' **←これが出ない** これだとflashより先にredirectしているので redirect_to root_path, alert: 'こちらからはログインできません' のほうがいいと思いました flash[:alert] = 'こちらからはログインできません' redirect_to root_path こうでもいいかもです
RThe

2021/01/10 23:50

ご回答頂きありがとうございます。 ご参考にさせて頂き、下記の内容で実装ができました お手数おかけ致しました。 def sign_out_user if current_user.admin sign_out_and_redirect(current_user) flash[:alert] = "こちらからはログインできません" end end
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問