LoginSignup
1
1

More than 3 years have passed since last update.

f.collection_selectの"選択して下さい"を選んだときのバリデーション

Posted at

1.やりたいこと

<%= f.collection_select :address_id, @customer.addresses, :id, :full_address, :include_blank => "選択してください" %>
フォームにおいて選択せずに一番上の"選択して下さい"を選んだ際にバリデーションをかけたい。

2.実装する

app/controllers/orders_controller.rb
 params[:order][:address_id] ==  ""
 flash[:notice] = "選択して下さい"
 redirect_to new_order_path

パラメータから送られている値を見ると""となっていたのでイコール文を""に変更。

3.終わりに

これを実装したかった時にQiitaやGoogleで検索しても全然出てこなかったので
投稿しました。
しっかりターミナルを見て返ってきてる値を確認しないといけないと痛感しました。

1
1
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
1
1