I almost have this perfected for AS, using the Carmen plugin nad
partial overrides for forms in AS. When someone picks a country, the
list of possible states to select from appears. If the current
record's country select box is the same as the record's, it gets that
record's state selected (if there is one) as well. It works
marvelously.
The ONLY problem, is that it doesn't update the state when the form
appears (i.e. in Edit or Create mode). One must first change the
country selected (then, often, change it back to what it initially
came up as) to invoke the proper state selections. Can someone please
tell me from my code below how to accomplish this -- I think this will
be a nice amendment to this group in case anyone ever does a search on
country & state. -Janna B.
# /views/customers/_state_form_column.html.erb
<p>
<dl class="" id="states">
<dt>
<label for="record_state_">State</label>
</dt>
<dd>
<% if @states !=nil %>
<% if @customer!=nil && @customer.state!=nil %>
<% s = @customer.state.strip %>
<% end %>
<select id="state" name="state">
<option></option>
<% for state in @states %>
<option value="<%= state[1] %>"
<% if state[1]==s %>
selected="selected"
<% end %>
>
<%= state[0] %></option>
<% end %>
</select>
<% else %>
<input type="text" size="30" name="record[state]" maxlength="255"
id="record_state_" class="state-input text-input" autocomplete="off"/
>
<% end %>
</dd>
</dl>
# /views/customers/_country_form_column.html.erb
<p>
<dl class="">
<dt>
<label for="record_country_">Country</label>
</dt>
<dd>
<% @customer == nil || @customer.country == nil ? s =
Carmen::country_name(Carmen.default_country) : s =
@customer.country.strip %>
<%= country_select :record, :country, [s], {},
{
:onChange => remote_function( :url => { :action =>
'update_states' }, :update => "states", :with => "'country=' +value")
} %>
</p>
</dd>
</dl>
#And finally, my controller:
class CustomersController < ApplicationController
active_scaffold :customer do |config|
...
end
..
def update_states
setstateslist
render :partial => 'state_form_column'
end
def setstateslist
q = params[:country]
if q==nil
if params[:id] != nil
@customer= Customer.find(params[:id])
end
if(@customer==nil)
q=Carmen.default_country
else
[email protected]
end
end
if(q!=Carmen.default_country)
q1 = q.gsub("_", " ")
q.capitalize!
if(q1!=q && q1!=nil)
q = q1.titleize #q1.split(' ').map {|w| w.capitalize }.join
(' ')
end
q = Carmen::country_code(q)
end
@states = Carmen::states(q)
end
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---