railsで下記のようなコードをinfo.rb
に書いているのですが、フォームに何も記入せずにsubmit
ボタンを押したらバリデーションのエラーメッセージが出ずに別のエラーが出てしまいます(=バリデーションをクリアしてしまう)。
Info.rb
class Info < ApplicationRecord validates :name, presence: true, length: { maximum: 30 } validates :collage, presence: true, length: { maximum: 30 } validates :department, presence: true, length: { maximum: 30 } validates :sex, presence: true, length: { maximum: 10 } validates :location, presence: true, length: { maximum: 10 } validates :avatar, presence: true, length: { maximum: 100 } belongs_to :user has_one_attached :avatar end
どうすればバリデーションに引っかかりますでしょうか?
フォームのコードは以下の通りです。
<div class="text-center"> <h1>ユーザー情報</h1> </div> <div class="row"> <div class="col-md-3 offset-md-1"> <%= form_for(@info) do |f| %> <%= render 'layouts/error_messages', model: f.object %> <% if @info.avatar.attached? %> <div class="center"> <%= image_tag @info.avatar.variant(resize: "330x300"), class: "prof-img" %> </div> <% end %> <div class="form-group"> <%= f.file_field :avatar, class: 'form-control' %><br> <%= f.submit '更新', :class=>'btn btn-primary btn-block' %> </div> <% end %> </div> <div class="col-md-6 offset-md-1"> <%= form_for(@info) do |f| %> <%= render 'layouts/error_messages', model: f.object %> <div class="form-group"> <%= f.label :name, '名前' %> <%= f.text_field :name, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :collage, '大学名' %> <%= f.text_field :collage, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :department, '学部名' %> <%= f.text_field :department, class: 'form-control' %> </div> <div class="form-group selection"> <%= f.label :sex, '性別' %> <%= f.select :sex, [["男性", "男性"], ["女性", "女性"], ["その他", "その他"]], :prompt => "選択してください", class: 'form-control' %> </div> <div class="form-group selection"> <%= f.label :location, '在住地' %> <%= f.select :location, [["北海道", "北海道"], ["青森県", "青森県"], ["岩手県", "岩手県"], ["宮城県", "宮城県"], ["秋田県", "秋田県"], ["山形県", "山形県"], ["福島県", "福島県"], ["茨城県", "茨城県"], ["栃木県", "栃木県"], ["群馬県", "群馬県"], ["埼玉県", "埼玉県"], ["千葉県", "千葉県"], ["東京都", "東京都"], ["神奈川県", "神奈川県"], ["新潟県", "新潟県"], ["富山県", "富山県"], ["石川県", "石川県"], ["福井県", "福井県"], ["山梨県", "山梨県"], ["長野県", "長野県"], ["岐阜県", "岐阜県"], ["静岡県", "静岡県"], ["愛知県", "愛知県"], ["三重県", "三重県"], ["滋賀県", "滋賀県"], ["京都府", "京都府"], ["大阪府", "大阪府"], ["兵庫県", "兵庫県"], ["奈良県", "奈良県"], ["和歌山県", "和歌山県"], ["鳥取県", "鳥取県"], ["島根県", "島根県"], ["岡山県", "岡山県"], ["広島県", "広島県"], ["山口県", "山口県"], ["徳島県", "徳島県"], ["香川県", "香川県"], ["愛媛県", "愛媛県"], ["高知県", "高知県"], ["福岡県", "福岡県"], ["佐賀県", "佐賀県"], ["長崎県", "長崎県"], ["熊本県", "熊本県"], ["大分県", "大分県"], ["宮崎県", "宮崎県"], ["鹿児島県", "鹿児島県"], ["沖縄県", "沖縄県"]], :prompt => "選択してください", class: 'form-control' %> </div> <%= f.submit '更新', class: 'btn btn-primary btn-block' %> <% end %> </div> </div>
infos_controller.rb
class InfosController < ApplicationController def new @info = Info.new end def create @info = Info.new(info_params) if @info.save flash[:success] = '更新しました。' redirect_to edit_info_url else flash.now[:danger] = '更新できませんでした。' render :edit end current_user.info = @info end def edit @info = current_user.info end def update @info = Info.find_by(user_id: params[:id]) if @info.update(info_params) flash[:success] = '更新しました。' redirect_to edit_info_url else flash.now[:danger] = '更新できませんでした。' render :edit end current_user.info = @info end private def info_params params.require(:info).permit(:name, :collage, :department, :sex, :location, :avatar) end end
回答1件
あなたの回答
tips
プレビュー