実現したいこと
railsで作成したフォームにバリデーションのエラーメッセージが表示されないので、表示したい
前提
バリデーションが正しく機能しているかは調べました。
バリデーションにあえて引っかかるようにフォームを入力し、データを送信したところ、データベースには保存されず、renderは通ります。
また、保存に成功した場合は、redirect_toでページを移動することは確認しました。
該当のソースコード
routes.rb
1Rails.application.routes.draw do 2 # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html 3 4 # Defines the root path route ("/") 5 # root "articles#index" 6 root to: "healths#index" 7 get "healths" => "healths#top" 8 get "healths/index" => "healths#index" 9 get "healths/new" => "healths#new" 10 post "healths" => "healths#create" 11 resources :healths, only:[:show] 12end
controller.rb
1class HealthsController < ApplicationController 2 before_action :set_q 3 def top 4 end 5 6 def index 7 @health=Health.all 8 @results=@q.result 9 end 10 11 def show 12 @health=Health.all 13 @results=@q.result 14 end 15 16 def new 17 @health=Health.new 18 @date=Date.today 19 end 20 21 def create 22 @health=Health.new(health_params) 23 @date=Date.today 24 @health.save_time=@date.strftime("%Y-%m-%d") 25 if @health.save 26 redirect_to(healths_url, notice: "送信を完了しました。") 27 else 28 render 'new' 29 end 30 end 31 32 def search 33 @results=@q.result 34 end 35 36 private 37 38 def health_params 39 params.require(:health).permit(:year, :school_class, :class_number, :name, :commute, :temperature, :etc) 40 end 41 42 def set_q 43 @q=Health.ransack(params[:q]) 44 end 45end 46
health.rb
1class Health < ApplicationRecord 2 def self.ransackable_attributes(auth_object=nil) 3 ["year", "school_class", "class_number", "name", "created_at", "commute", "temperature", "etc", "save_time"] 4 end 5 validates :year,:school_class, :class_number, :name, :commute, :temperature, presence: true 6end
new.html.erb
1<h1>健康観察アプリ</h1> 2<% require "date" %> 3<%= link_to "ホーム画面に戻る", "/healths" %>|<%= link_to "全体検索を行う", "/healths/show" %>|<%= link_to "個別検索を行う", "/healths/index"%> 4<% # コントローラーで生成されたインスタンスメソッド@healthにデータを付与 %> 5<%= form_with model: @health, local: true do |form| %> 6 <%= render "shared/error_messages"%> 7 <table> 8 <tr> 9 <td>学年(選択必須)</td><td><%= form.select :year, [["1年","1年"], ["2年","2年"], ["3年","3年"]],{include_blank: '選択してください'} %></td> 10 </tr> 11 <tr> 12 <td>組(選択必須)</td><td><%= form.select :school_class, [["A組","A組"], ["B組","B組"], ["C組","C組"],["D組","D組"], ["E組","E組"], ["F組","F組"],["G組","G組"], ["H組","H組"], ["I組","I組"]],{include_blank: '選択してください'} %></td> 13 </tr> 14 <tr> 15 <td>出席番号(選択必須)</td><td><%= form.select :class_number, [["1番",1], ["2番",2], ["3番",3],["4番",4], ["5番",5], ["6番",6],["7番",7], ["8番",8], ["9番",9],["10番",10], 16 ["11番",11], ["12番",12], ["13番",13],["14番",14], ["15番",15], ["16番",16],["17番",17], ["18番",18], ["19番",19],["20番",20], 17 ["21番",21], ["22番",22], ["23番",23],["24番",24], ["25番",25], ["26番",26],["27番",27], ["28番",28], ["29番",29],["30番",30], 18 ["31番",31], ["32番",32], ["33番",33],["34番",34], ["35番",35], ["36番",36],["37番",37], ["38番",38], ["39番",39],["40番",40],],{include_blank: '選択してください'} %></td> 19 </tr> 20 <tr> 21 <td>氏名(入力必須)</td><td><%= form.text_field :name, {include_blank: '入力して下さい'} %></td></td> 22 </tr> 23 </table> 24 <hr> 25 <%= form.radio_button :commute, "登校" %><%= form.label :commute, "登校", value: "登校"%> 26 <%= form.radio_button :commute, "遅刻" %><%= form.label :commute, "遅刻", value: "遅刻"%> 27 <%= form.radio_button :commute, "欠席" %><%= form.label :commute, "欠席", value: "欠席"%> 28 <table> 29 <tr> 30 <td>測定した体温(半角)</td><td><%= form.text_field :temperature %>度</td> 31 </tr> 32 <tr> 33 <td>その他</td><td><%= form.text_area :etc %></td> 34 </tr> 35 </table> 36 <%= form.submit value: "提出"%> 37<% end %>
shared/_error_messages.html.erb
1<% #エラーメッセージの表示 %> 2<% if @health.errors.any? %> 3 <ul> 4 <% @health.errors.full_messages.each do |message| %> 5 <li><%= message %></li> 6 <% end %> 7 </ul> 8<% end %>
試したこと
前提でも述べたように、バリデーションは正しく通っていますが、エラーメッセージが出ないので、ネットで調べていくつかの解決法を試しましたが、解決はしませんでした。
具体的には、form_withにおいてlocal: trueを入れると解決するなどがありましたが、該当のソースコードでも記述してある通り、正しくタイプできていますが、解決はしていない状態です。

回答1件
あなたの回答
tips
プレビュー