Como faço para converter este dropdown para um f.select no Rails?
Eu sou um iniciante em Rails e estou trabalhando em um projeto preexistente do Rails 2. No meu aplicativo, tentei converter um campo suspenso select em um form_handler f.select, mas estou recebendo este erro:
undefined method `location.masterlocation.name
Aqui está minha tentativa:
<% 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")%>
Aqui está o campo suspenso original:
<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>
Agradeço antecipadamente por sua ajuda!
editar 1
Eu me aproximei muito mais desse código:
<%= f.select :start, options_for_select(@itinerary.locations.map{ |c| [c.masterlocation.name, c.masterlocation.street_address]}),{}, :id=>"startdrop", :name=>"startthere" %>
O problema é que eu quero incluir a cidade, estado e zip no valor, todos separados por vírgulas. Alguma idéia sobre como fazer isso?
<%= 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" %>
ISSO FUNCIONA!
Ajudante Maptry:
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
Visão
<%= f.select :start, options_for_select(@itinerary.locations),{}, :id=>"startdrop", :name=>"startthere" %>