5
3

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 5 years have passed since last update.

railsにてf.selectにclass設定

5
Last updated at Posted at 2019-04-29

はじめに

初学者のアウトプット記事です。
ご指摘アドバイスあればよろしくお願いします。

本日のハマりです。

じゃん!!!!

railsでf.selectにclass設定できなくハマる

# html.Haml表記です
= form_with url: root_path, html: { class: "signup" } do |f|
  = f.select :card, [[ "01","01"], [ "02", "02" ], [ "03", "03" ]],  class: 'signup__box'

# これだとclass設定できてません。

f.text_fieldの時はこんな感じでclass設定ができてたのですが、f.selectだとできない。。。

で、早速対処法なんですが、

class設定

= form_with url: root_path, html: { class: "signup" } do |f|
  = f.select :card, [[ "01","01"], [ "02", "02" ], [ "03", "03" ]], {},  class: 'signup__box'

# これでclass設定できます

なんと。
{},これ (空のハッシュ)を先に記述するだけでclass設定できました。

参考

公式ドキュメントも読みましたがイマイチ原因が分からず、こちらの記事を参考にしました。
解決できたものの、まだまだ腑に落ちないのでアドバイスや情報あれば教えてほしいです。

railsでf.selectにclassを設定する
https://qiita.com/nakanoyoshiki/items/e87a6238f8febbeb208a

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?