0

I am editing a Rails 2 application. In it, a user submits a form that includes a dropdown menu, and that information creates a record in my database. When the user goes to edit the record, I want to show the saved "selected" option in the dropdown menu. I keep getting errors, however. Here is my set-up:

View Select Field

    <% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
    <%= f.select :start, options_for_select(@itinerary.locations), {:include_blank => true}, {:id=>"startdrop" } %>      

Form Helper

    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

I have tried making the view look like this:

    <%= f.select :start, options_for_select(@itinerary.locations, @newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %> 

But I get wrong number of arguments (2 for 1)) for that line. Also tried

    <%= f.select :start, options_for_select(@itinerary.locations), {:selected => @newsavedmap.start, :include_blank => true}, {:id=>"startdrop" } %> 

But nothing is preselected when I go to edit the saved map. I've tried following these links, and keep striking out. Appreciate any help you have to offer!

Rails select helper - Default selected value, how? , Rails form_for select tag with option selected , Rails select helper - Default selected value, how?

Community
  • 1
  • 1
KDP
  • 634
  • 1
  • 11
  • 31

2 Answers2

1

You could try something like this in your helper

   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

then call field like you were trying before.

<%= f.select :start, options_for_select(@itinerary.locations, @newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
David Barlow
  • 4,914
  • 1
  • 28
  • 24
  • thanks so much for your help. Unfortunately, this results in an error: undefined local variable or method `location'. I think the error refers to the helper, though it points to the view code you provided. – KDP Sep 03 '13 at 21:57
  • @KDP I have updated my answer, one of the variables was named incorrect, 'location' should have been 'l' – David Barlow Sep 04 '13 at 08:09
  • Thanks, Barlow! If you've got the time, I'd really appreciate help with some of my other Rails questions on SO (most recent http://stackoverflow.com/questions/18622224/moving-options-from-one-select-field-to-another-using-form-handlers-in-rails and this one http://stackoverflow.com/questions/18603320/why-isnt-rails-recognizing-nil-if-blank-in-my-form) – KDP Sep 06 '13 at 23:14
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" } %>
royalghost
  • 2,773
  • 5
  • 23
  • 29
  • thanks for the comment, but when I create a record with this code and then try to edit it, no option is selected. Any ideas why? – KDP Sep 03 '13 at 21:54
  • Are you sure your field is updated? Can you do a SQL query to see if the value is there in the database itself? – royalghost Sep 04 '13 at 02:22