grouped_collection_select collection filter

30 views
Skip to first unread message

Rodrigo Lueneberg

unread,
Oct 20, 2013, 5:25:53 PM10/20/13
to rubyonra...@googlegroups.com
I am having a hard time to alter the where condition of the
grouped_collection_select collection: Country.where("name='Brazil'") not
to use hard coded value, but the form value.

This is how it is working:
<%= f.grouped_collection_select(:state,
Country.where("name='Brazil'").order(:name), :states, :name, :id, :name)
%>

This is what I am trying to do:
<%= f.grouped_collection_select(:state,
Country.where(:name=>:state).order(:name), :states, :name, :id, :name)
%>

<%= form_for(@address, :url => {:action => 'update', :controller =>
'addresses', :id => @address.id}) do |f| %>
<%= render "shared/error_messages", :target => @address %>

<table id="TbUser" summary="User form fields">
<tr>
<th colspan="2" style="background-color: #fff;
text-align:left">Dados do Cliente</th>
</tr>
<tr>
<th>Endereço</th>
<td><%= f.text_field(:address) %></td>
</tr>
<tr>
<th>Bairro</th>
<td><%= f.text_field(:neighborhood) %></td>
</tr>
<tr>
<th>Cidade</th>
<td><%= f.text_field(:city) %></td>
</tr>
<tr>
<th>Estado</th>

<td><%= f.grouped_collection_select(:state,
Country.where("name='Brazil'").order(:name), :states, :name, :id, :name)
%></td>
</tr>
<tr>
<th>Cep</th>
<td><%= f.text_field(:zipcode) %></td>
</tr>
<tr>
<th>País</th>
<td><%= f.text_field(:country) %></td>
</tr>
<tr>
<th colspan="2" style="background-color:white; text-align:
center"><%= submit_tag("Atualizar") %></th>
</tr>
</table>
<div>

</div>
<% end %>

--
Posted via http://www.ruby-forum.com/.

Kang Cao

unread,
Oct 21, 2013, 6:26:29 AM10/21/13
to rubyonra...@googlegroups.com
if you want choose states depending countries, you have to use the ajax way. That is  when rails renders the page, it doesn't know which country you will choose. Thus you have to write javascript code to watch the country field, when it changes, the code sends messages to server and use reponse messages to renew the options.
Reply all
Reply to author
Forward
0 new messages