0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Rails]collection_selectにclassをつける

Posted at

はじめに

本記事では、collection_selectclassをつける方法を記述します。

うまくいかないコード

第5引数、第6引数にオプションを記述し、第7引数にclass名を指定しています。

<%= search_form_for @q, url: search_foods_path, class: "food-search", id:"pull-down" do |f| %>
  <%= f.label :shop_name_or_shop_name_kana_or_food_name_or_station_cont, '店名・メニュー名・駅名', class: "search-label" %><br/>
  <%= f.text_field :shop_name_or_shop_name_kana_or_food_name_or_station_cont, placeholder: "KEYWORD", class: "search-text" %><br/>
  <%= f.label :user_nickname_cont, 'ユーザー名', class: "search-label" %><br/>
  <%= f.text_field :user_nickname_cont, placeholder: "KEYWORD", class: "search-text" %><br/>
  <%= f.label :meal_type_id_eq, '種類', class: "search-label" %><br/>
 <%# ここ↓ %>
  <%= f.collection_select :meal_type_id_eq, MealType.all, :id, :name, include_blank: '--', disabled: 0, class: "search-text" %><br/>
  <%= f.label :spicy_level_id_eq, '辛さ', class: "search-label" %><br/>
  <%= f.collection_select :spicy_level_id_eq, SpicyLevel.all, :id, :name, include_blank: '--', class: "search-text" %><br/>
  <%= f.submit "SEARCH", class: "search-submit" %><br/>
<% end %>

ここが間違えているようです。

  <%= f.collection_select :meal_type_id_eq, MealType.all, :id, :name, include_blank: '--', disabled: 0, class: "search-text" %><br/>
  <%= f.label :spicy_level_id_eq, '辛さ', class: "search-label" %><br/>
  <%= f.collection_select :spicy_level_id_eq, SpicyLevel.all, :id, :name, include_blank: '--', class: "search-text" %><br/>
  <%= f.submit "SEARCH", class: "search-submit" %><br/>

修正したコード

第5引数、第6引数にしていたオプションを {}で記述することで、
第5引数にまとめました。
また、第6引数にclass名を指定してましたところ、うまくCSSが効きました。

f.collection_selectの場合は第6引数をclass名にする必要があるらしいです。
オプションの後に{}無しで記述すると、HTMLのオプションと認識されるようです。

<%= search_form_for @q, url: search_foods_path, class: "food-search", id:"pull-down" do |f| %>
  <%= f.label :shop_name_or_shop_name_kana_or_food_name_or_station_cont, '店名・メニュー名・駅名', class: "search-label" %><br/>
  <%= f.text_field :shop_name_or_shop_name_kana_or_food_name_or_station_cont, placeholder: "KEYWORD", class: "search-text" %><br/>
  <%= f.label :user_nickname_cont, 'ユーザー名', class: "search-label" %><br/>
  <%= f.text_field :user_nickname_cont, placeholder: "KEYWORD", class: "search-text" %><br/>
  <%= f.label :meal_type_id_eq, '種類', class: "search-label" %><br/>
 <%# ここ↓ %>
  <%= f.collection_select :meal_type_id_eq, MealType.all, :id, :name, {include_blank: '--', disabled: 0}, class: "search-text" %><br/>
  <%= f.label :spicy_level_id_eq, '辛さ', class: "search-label" %><br/>
  <%= f.collection_select :spicy_level_id_eq, SpicyLevel.all, :id, :name, {include_blank: '--'}, class: "search-text" %><br/>
  <%= f.submit "SEARCH", class: "search-submit" %><br/>
<% end %>

以上です。

終わりに

短時間で解決できましたが、やや特殊なケースかなと思い、記事にしました。

以下参考サイトです。
collection_select not inserting 'class' attribute?
collection_selectにclassをつける方法
Railsドキュメント
collection_selectタグでclassが反映されない

明日も頑張ります!!!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?