前提・実現したいこと
hamlでタイトルにあるコードを書く方法が知りたい。
現在、個人アプリの開発にてカテゴリー機能を実装しています。
その中で、
<option value="<%= c.id %>"><%= c.name %></option>
<option><%= c.name %></option>
このような文法の場合、hamlではどのように書くのでしょうか?
%option{value: "hoge"}
みたいに書くのはできるんですが、それよりも高度なコードになっているので、なかなか自分で実装できずに困っています。
ちなみに参考にしている記事は、以下の記事のセレクトボックスを表示するです。
https://qiita.com/kenzoukenzou104809/items/07f6b5f7fdcf2761ac8f
【追記】
該当コード
_form.haml.html
= form_for(@post) do |f|
.field
.field-label
= f.label :title, "snowB"
.field-input
= f.text_field :title
.field
.field-label
= f.label :body, "ココがポイント!"
.field-input
= f.text_area :body
.field
.field-label
= f.label :image, "snowB写真"
.field-input
= f.attachment_field :image
.category
= render partial: "category"
.actions
= f.submit "投稿する", class: "btn"
_category.haml.html
.categorySelect
%select#pararent_category
- @parent_categoryies.each do |c|
= options_for_select([['= c.id', =c.name]])
エラー文
_category.html.haml:4: syntax error, unexpected '=', expecting ']' ...ptions_for_select([['= c.id', =c.name]]) ... ^
_category.html.haml:4: syntax error, unexpected ']', expecting ')' ...for_select([['= c.id', =c.name]]) ... ^
_category.html.haml:5: syntax error, unexpected keyword_end, expecting ')' ...t.buffer << ("\n".freeze);; end;; _hamlout.buffer << ("</sel... ... ^~~
_category.html.haml:5: syntax error, unexpected keyword_ensure, expecting ')' ...:Util.html_safe(_erbout);ensure;@haml_buffer = @haml_buffer.... ... ^~~~~~
_category.html.haml:5: syntax error, unexpected keyword_end, expecting ')' ...uffer.upper if @haml_buffer;end; ... ^~~
_category.html.haml:8: syntax error, unexpected keyword_end, expecting ')' end ^~~
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
こういう場合私はoptions_for_selectを使います
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.23%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2020/08/25 11:04
ご教示いただいた以下の記事にて試しにコード書いてみました。
https://railsdoc.com/page/options_for_select
記事にある参考コード
```
options_for_select([['Japanese', 'JPN']], 'JPN')
```
```
%select#pararent_category
- @parent_categoryies.each do |c|
= options_for_select([['= c.id', =c.name]])
```
しかし、エラーが出てしまい、なかなか思うようにコードが書けません。
質問のコードをhamlで実現するには、どのようなコードを書けば良いのでしょうか?
2020/08/25 11:48
2020/08/25 11:54
options_for_select の引数は 実際の値の 配列か配列の配列かHashの配列 です。
options_fr_select自体は select_tag や select の引数です。
もしかして Railsっぽい書き方していないのかな。
view全体を載せて
2020/08/25 12:22
view全体のコード
_form.haml.html
```
= form_for(@post) do |f|
.field
.field-label
= f.label :title, "snowB"
.field-input
= f.text_field :title
.field
.field-label
= f.label :body, "ココがポイント!"
.field-input
= f.text_area :body
.field
.field-label
= f.label :image, "snowB写真"
.field-input
= f.attachment_field :image
.category
= render partial: "category"
.actions
= f.submit "投稿する", class: "btn"
```
_category.haml.html
```
.categorySelect
%select#pararent_category
- @parent_categoryies.each do |c|
= options_for_select([['= c.id', =c.name]])
```
こちらのコードでは、構文エラーが出ています。
以下エラー文です。
_category.html.haml:4: syntax error, unexpected ']', expecting ')' ...for_select([['= c.id', =c.name]])
_category.html.haml:5: syntax error, unexpected keyword_end, expecting ')' ...t.buffer << ("\n".freeze);; end;; _hamlout.buffer << ("</sel... ... ^~~
_category.html.haml:5: syntax error, unexpected keyword_ensure, expecting ')' ...:Util.html_safe(_erbout);ensure;@haml_buffer = @haml_buffer.... ... ^~~~~~
_category.html.haml:5: syntax error, unexpected keyword_end, expecting ')' ...uffer.upper if @haml_buffer;end; ... ^~~
_category.html.haml:8: syntax error, unexpected keyword_end, expecting ')' end ^~~
2020/08/25 14:18
2020/08/25 16:06