db
にはinteger
クラスの値を入れているが、view
では文字
で表示したい為、hash
を利用しています。値の量が多いことから、選択肢をmodel
内に移植しているのですが、自分のやり方だと、デフォルトで選択した値がセットされません(例えば、edit/updateの時に初期値に戻って表示される)
options_from_collection_for_select
の第4引数
に誤りがあるのでしょうか。
何か他に良いやり方があればご教示頂きたくお願いします。
ruby
1## データベース 2Table name: lessons 3id :integer 4price :integer 5 6##_form.html.erb 7<%= f.select :price, options_from_collection_for_select(Lesson::PRICE_LIST, :last, :first, params[:price] ), {}, {class:"form-control"}%> 8 9## model 10class Lesson < ApplicationRecord 11 PRICE_LIST = { 12 "無料" => 0, "1,000円" => 1000, "1,500円" => 1500, "2,000円" => 2000, 13 "2,500円" => 2500, "3,000円" => 3000, "3,500円" => 3500, "4,000円" => 4000, 14 "4,500円" => 4500, "5,000円" => 5000, "6,000円" => 6000, "7,000円" =>7000, 15 "8,000円" => 8000, "9,000円" => 9000, "10,000円" => 10000, "11,000円" => 11000, 16 "12,000円" => 12000, "13,000円" => 13000, "14,000円" => 14000, 17 "15,000円" => 15000, "16,000円" => 16000, "17,000円" => 17000, "18,000円" => 18000, 18 "19,000円" => 19000, "20,000円" => 20000 19 } 20end

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。