やりたい事
・ check_boxを作る。 環境(AWSのC9)
###現在の状況
novel_listsテーブルにnovel_keyword:stringカラムを作る
novel_list.rb enum novel_keyword: { status_difference: 1, year_difference: 2, non_love: 3, villain_daughter: 4, school_love: 5, original_senki: 6, } enumを使って、配列を作る。
<%= form_with(model: @novel_list, local: true) do |f| %> <%= f.check_box :novel_keyword, :status_difference, checked: "checked" %> <%= f.label :novel_keyword, "身分差", class: "status_difference" %> <%= f.check_box :novel_keyword, :year_difference %> <%= f.label :novel_keyword, "年の差", class: "year_difference" %> <%= f.check_box :novel_keyword, :non_love %> <%= f.label :novel_keyword, "非恋", class: "non_love" %> <%= f.check_box :novel_keyword, :villain_daughter %> <%= f.label :novel_keyword, "悪役令嬢", class: "villain_daughter" %> <%= f.check_box :novel_keyword, :school_love %> <%= f.label :novel_keyword, "スクールラブ", class: "school_love" %> <%= f.check_box :novel_keyword, :original_senki %> <%= f.label :novel_keyword, "オリジナル戦記", class: "original_senki" %> form_with内にチェックボックスを書く。
###エラー内容
NoMethodError in NovelLists#new Showing /home/ec2-user/environment/novel-favorites/app/views/novel_lists/new.html.erb where line #7 raised: undefined method `merge' for :status_difference:Symbol Extracted source (around line #7): <label for="title">タイトルを入力してください。</label></div><%= f.text_field :title, class: 'form-control', placeholder: "タイトルを20文字以内で入力してください" %> <h4 class="keyword_image">キーワード</h4> <%= f.check_box :novel_keyword, :status_difference, checked: "checked" %> <%= f.label :novel_keyword, "身分差", class: "status_difference" %> <%= f.check_box :novel_keyword, :year_difference %> <%= f.label :novel_keyword, "年の差", class: "year_difference" %>
###試した事。
radio_buttonにすると上手く行くので、checkboxの使い方に問題ありと思い検索をして。 check_box_tag にしたり、 colectionでnovel_keywordカラムに入ってるのを全て持ってこようと思ったが、 調べても、モデルからUser.all のように持ってきたりするものばかりで、断念。 <%= f.radio_button :novel_keyword, :status_difference, checked: "checked" %> <%= f.label :novel_keyword, "身分差", class: "status_difference" %>
アドバイス頂けると幸いです。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/24 13:49