Rails 2 アプリケーションを編集しています。その中で、ユーザーはドロップダウン メニューを含むフォームを送信し、その情報によってデータベースにレコードが作成されます。ユーザーがレコードを編集するときに、保存された「選択済み」オプションをドロップダウン メニューに表示したいと考えています。ただし、エラーが発生し続けます。これが私のセットアップです:
ビュー選択フィールド
<% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.select :start, options_for_select(@itinerary.locations), {:include_blank => true}, {:id=>"startdrop" } %>
フォーム ヘルパー
def options_for_select(locations)
locations.map do |l|
content_tag "option", l.masterlocation.name, location_option_attributes(l)
end.join("\n")
end
private
def location_option_attributes(location)
{
:value => "#{location.masterlocation.street_address}, #{location.masterlocation.city}, #{location.masterlocation.state}, #{location.masterlocation.zip}",
:id => location.masterlocation.id,
:"data-masterlocation-name" => location.masterlocation.name,
:"data-masterlocation-id" => location.masterlocation.id,
:"data-masterlocation-latitude" => location.masterlocation.latitude,
:"data-masterlocation-longitude" => location.masterlocation.longitude
}
end
ビューを次のようにしてみました。
<%= f.select :start, options_for_select(@itinerary.locations, @newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
しかし、その行の引数の数が間違っています (1 に対して 2))。また試した
<%= f.select :start, options_for_select(@itinerary.locations), {:selected => @newsavedmap.start, :include_blank => true}, {:id=>"startdrop" } %>
しかし、保存したマップを編集しようとすると、何も選択されていません。私はこれらのリンクをたどってみました。あなたが提供しなければならない助けに感謝します!
Rails select helper - デフォルトで選択された値、どのように? 、オプションが選択されたRails form_for selectタグ、Rails select helper - デフォルトで選択された値、どのように?