0

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 - デフォルトで選択された値、どのように?

4

2 に答える 2

1

ヘルパーでこのようなことを試すことができます

   def options_for_select(locations, selected=nil)
      locations.map do |l|
        tag_options = location_option_attributes(l)
        if l == selected
         tag_options[:selected] = "selected" 
        end
        content_tag "option", l.masterlocation.name, tag_options
      end.join("\n")
    end

次に、以前に試していたようにフィールドを呼び出します。

<%= f.select :start, options_for_select(@itinerary.locations, @newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
于 2013-09-03T01:56:25.073 に答える
0

You can pass one more parameters to select the value like this:

<%= f.select :start, options_for_select(@itinerary.locations), :selected => @newsavedmap.start,  {:include_blank => true}, {:id=>"startdrop" } %>
于 2013-09-03T09:04:11.487 に答える