「Rails」Haml記載時のフォームへルパーに対してのclass名の指定ができません。ご教示お願いします。
- 評価
- クリップ 0
- VIEW 218
Hamlファイルにclass名の指定をしたいのですが、どうしてもエラー文が表示されてしまいます。コードは下記に記載しています。今回の場合、いろんな場合を試してみました。クラス名の記述方法に関しては、下記の記事を参考にさせていただきました。
リンク内容
fieldクラスの、form.labelと、form.date_selectにクラス名を追加したい状態です。この二つのタグはそれぞれ違う記述法で試していますが、どちらのやり方もうまく行きません。この記述に対するエラー文がhamlファイルのしたにコードを挿入しておきます。また、エラー文の画像も挿入しておきました。確認お願いします。
リンク内容
_form.html.haml
= form_with(model: article, local: true) do |form|
- if article.errors.any?
#error_explanation
%h2
= pluralize(article.errors.count, "error")
prohibited this article from being saved:
%ul
- article.errors.full_messages.each do |message|
%li= message
.field
= form.label{ :post_date, :class => 'f' }
= form.date_select :post_date{ :class 'a' }
.field
= form.label :title
= form.text_field :title
.field
= form.label :content
= form.text_area :content
.actions
= form.submit
ActionView::SyntaxErrorInTemplate in ArticlesController#new
Encountered a syntax error while rendering template: check = form_with(model: article, local: true) do |form| - if article.errors.any? #error_explanation %h2 = pluralize(article.errors.count, "error") prohibited this article from being saved: %ul - article.errors.full_messages.each do |message| %li= message .field = form.label{ :post_date, :class => 'f' } = form.date_select :post_date{ :class 'a' } .field = form.label :title = form.text_field :title .field = form.label :content = form.text_area :content .actions = form.submit
Extracted source (around line #11):
9
10
11
12
13
14
%li= message
.field
= form.label{ :post_date, :class => 'f' }
= form.date_select :post_date{ :class 'a' }
.field
= form.label :title
また、エラーに対するターミナルの記述も載せておきます。
ActionView::SyntaxErrorInTemplate (Encountered a syntax error while rendering template: check = form_with(model: article, local: true) do |form|
- if article.errors.any?
#error_explanation
%h2
= pluralize(article.errors.count, "error")
prohibited this article from being saved:
%ul
- article.errors.full_messages.each do |message|
%li= message
.field
= form.label{ :post_date, :class => 'f' }
= form.date_select :post_date{ :class 'a' }
.field
= form.label :title
= form.text_field :title
.field
= form.label :content
= form.text_area :content
.actions
= form.submit
):
1: = form_with(model: article, local: true) do |form|
2: - if article.errors.any?
3: #error_explanation
4: %h2
5: = pluralize(article.errors.count, "error")
6: prohibited this article from being saved:
7: %ul
8: - article.errors.full_messages.each do |message|
9: %li= message
10: .field
11: = form.label{ :post_date, :class => 'f' }
12: = form.date_select :post_date{ :class 'a' }
13: .field
14: = form.label :title
15: = form.text_field :title
16: .field
17: = form.label :content
18: = form.text_area :content
19: .actions
20: = form.submit
app/views/articles/_form.html.haml:11: syntax error, unexpected ',', expecting '}'
app/views/articles/_form.html.haml:11: syntax error, unexpected '}', expecting ')'
app/views/articles/_form.html.haml:12: syntax error, unexpected '{', expecting ')'
app/views/articles/_form.html.haml:12: syntax error, unexpected '}', expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected end, expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected ensure, expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected end, expecting ')'
app/views/articles/_form.html.haml:11: syntax error, unexpected ',', expecting '}'
app/views/articles/_form.html.haml:11: syntax error, unexpected '}', expecting ')'
app/views/articles/_form.html.haml:12: syntax error, unexpected '{', expecting ')'
app/views/articles/_form.html.haml:12: syntax error, unexpected '}', expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected end, expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected ensure, expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected end, expecting ')'
app/views/articles/_form.html.haml:11: syntax error, unexpected ',', expecting '}'
app/views/articles/_form.html.haml:11: syntax error, unexpected '}', expecting ')'
app/views/articles/_form.html.haml:12: syntax error, unexpected '{', expecting ')'
app/views/articles/_form.html.haml:12: syntax error, unexpected '}', expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected end, expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected ensure, expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected end, expecting ')'
app/views/articles/_form.html.haml:11: syntax error, unexpected ',', expecting '}'
app/views/articles/_form.html.haml:11: syntax error, unexpected '}', expecting ')'
app/views/articles/_form.html.haml:12: syntax error, unexpected '{', expecting ')'
app/views/articles/_form.html.haml:12: syntax error, unexpected '}', expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected end, expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected ensure, expecting ')'
app/views/articles/_form.html.haml:21: syntax error, unexpected end, expecting ')'
app/views/articles/new.html.haml:2
問題の確認と解決案があれば、どなたかご教授お願いします!
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
0
= form.date_select :post_date, class: 'a'
でどうでしょうか?
もしダメなら
= form.date_select :post_date, {}, class: 'a'
こちらも試してみてください
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.22%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2020/08/21 20:26 編集
そもそものクラス名の指定方法がおかしいのでは、なくて別に原因がある可能性はありそうですか?どうかよろしくお願いします。
https://gyazo.com/8f3476f2d604b14b51ddf435033bd1fa
https://gyazo.com/9013c18c8d2459b27fb2804ffcdd5eec
2020/08/21 20:50 編集
label: 'a' ですね
2020/08/21 21:57
https://gyazo.com/c82ad4e3f4a338d021c4b27c2983ba4c
このような記載法でうまくいかないのは、、、原因がわかりません。
2020/08/22 06:51 編集
:label 'f'
僕
label: 'f'
` : ` の位置に注目してください
2020/08/22 16:07
nasuk47さんの指摘通りに直した結果、labelにクラス名が付与されました。
https://gyazo.com/32efb21c1d9c3e3c74b22a275089afe1
https://gyazo.com/7cafb73fee135ec5838536ba3446722d
しかし、labelの下のdate_selectにクラス名が付与されているのかどうかがわからない、、
その前に、date_selectが見つかりませんでした。どこにあるのでしょうか?
すみません、恐縮ながら教えてください。
2020/08/22 16:23
なのでselectタグにクラスがついていたらついているのですがついていませんね。
回答の方にも書いてありませが
`= form.date_select :post_date, {}, class: 'a' `
のように書くとどうでしょうか??
2020/08/22 19:33
の両端についているグレイヴアクセントの必要でしょうか?
https://gyazo.com/41bbb04706071c6c1ee6b9ee6e61e48c
上記のような表示になってしまいます。
2020/08/22 20:08
https://qiita.com/t_oginogin/items/519fb52e1708e26a8b73
date_select クラスと調べると大体実装方法載っています。
他のformも同様に(collection_selectなど)
2020/08/27 17:19
調べてみます。