前提・実現したいこと
Ruby On Railsでgemのdeviseを導入し、ログイン機能を実装しました。
実装時に、新規会員登録のviewを、デフォルトのform_forを使用したものから、form_withを使用したものに書き換えました。
この結果、新規会員登録時(及びログイン時)の入力内容が、バリデーションに反する内容であっても、単に入力し直しとなるだけで、エラーメッセージの表示が出ません。
ついては、エラーメッセージが表示されるようにしたいです。
該当のソースコード
devise/registrations/new.html.slim
h2 新規登録 = form_with model: @user, url: user_registration_path, id: 'new_user', class: 'new_user' do |f| = render "devise/shared/error_messages", resource: resource .form-group = f.label :department = f.text_field :department, autofocus: true, class: 'form-control' .form-group = f.label :position = f.text_field :position, autofocus: true, class: 'form-control' .form-group = f.label :name = f.text_field :name, autofocus: true, class: 'form-control' .form-group = f.label :email = f.text_field :email, autofocus: true, class: 'form-control' .form-group = f.label :password - if @minimum_password_length em | ( = @minimum_password_length | 字以上) = f.password_field :password, autofocus: true, class: 'form-control' .form-group = f.label :password_confirmation = f.password_field :password_confirmation, autofocus: true, class: 'form-control' = f.submit class: 'btn btn-primary'
devise/shared/_error_messages.html.slim
- if resource.errors.any? #error_explanation h2 = I18n.t("errors.messages.not_saved",count: resource.errors.count, resource: resource.class.model_name.human.downcase) ul - resource.errors.full_messages.each do |message| li = message
layouts/application.html.slim
doctype html html head title | DisasterDamageApp = csrf_meta_tags = csp_meta_tag = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' = javascript_include_tag 'application', 'data-turbolinks-track': 'reload' body .app-title.navbar.navbar-expand-md.navbar-light.bg-light .navbar-brand = link_to "農災App", root_path ul.navbar-nav.ml-auto - if user_signed_in? li.nav-item = link_to '被害一覧', damages_path, class: 'nav-link' li.nav-item = link_to '被害登録', new_damage_path, class: 'nav-link' li.nav-item = link_to 'マイページ', user_path(current_user.id), class: 'nav-link' li.nav-item = link_to 'ログアウト', destroy_user_session_path, method: :delete, class: 'nav-link' - else li.nav-item= link_to '新規登録', new_user_registration_path, class: 'nav-link' li.nav-item= link_to 'ログイン', new_user_session_path, class: 'nav-link' .container - if flash.notice.present? .alert.alert-success = flash.notice - if flash.alert.present? .alert.alert-error = flash.alert = yield
models/user.rb
class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :damages validates :department, presence: true validates :position, presence: true validates :name, presence: true validates :email, presence: true, uniqueness: true end
補足情報(FW/ツールのバージョンなど)
・viewはslimで作成しています。
・bootstrapを導入しています。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。