Jak przekonwertować listę rozwijaną na f.select w Railsach?
Jestem początkującym użytkownikiem Rails i pracuję nad istniejącym wcześniej projektem Rails 2. W mojej aplikacji próbowałem przekonwertować pole rozwijane select na pole form_handler f.select, ale otrzymuję ten błąd:
undefined method `location.masterlocation.name
Oto moja próba:
<% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.select(:start, options_from_collection_for_select(@itinerary.locations, "location.masterlocation.street_address, location.masterlocation.city, location.masterlocation.state, location.masterlocation.zip", "location.masterlocation.name"), :id => "startdrop")%>
Oto oryginalne pole rozwijane:
<select id="startdrop">
<option value="">
<% for location in @itinerary.locations %>
<option value="<%= location.masterlocation.street_address %> <%= location.masterlocation.city %>, <%= location.masterlocation.state %>, <%= location.masterlocation.zip %>"><%= location.masterlocation.name %></option>
<% end %>
</select>
Z góry dziękuje za twoją pomoc!
edytuj 1
Korzystam z tego kodu znacznie bliżej:
<%= f.select :start, options_for_select(@itinerary.locations.map{ |c| [c.masterlocation.name, c.masterlocation.street_address]}),{}, :id=>"startdrop", :name=>"startthere" %>
Problem polega na tym, że chcę dodać miasto, stan i zip do wartości, wszystkie rozdzielone przecinkami. Jakieś pomysły na to, jak to zrobić?
<%= f.select :start, options_for_select(@itinerary.locations.map{ |c| [c.masterlocation.inst_name, c.masterlocation.street_address AND , AND c.masterlocation.city AND , AND c.masterlocation.state AND, AND c.masterlocation.zip]}),{}, :id=>"startdrop", :name=>"startthere" %>
TO DZIAŁA!
Maptry Helper:
module MaptryHelper
def options_for_select(locations)
locations.map do |location|
[location.masterlocation.name, location_string(location.masterlocation)]
end
end
def location_string(masterlocation)
"#{masterlocation.street_address}, #{masterlocation.city}, #{masterlocation.state}, #{masterlocation.zip}"
end
end
Widok
<%= f.select :start, options_for_select(@itinerary.locations),{}, :id=>"startdrop", :name=>"startthere" %>