I have implemented the AJAX observe_field to filter the state/province
options
in my address form. This works great except when I submit the form,
@params
doesn’t contain the select element.
_address_form.rhtml relevant snippet:
<%= collection_select(:address, “country”, Country.find(:all, :order =>
“position”), :code, :name, {:prompt => “-- Select Country --”}, {:class
=>
“siteText”, :style => “width: 175px;”}) %>
<%= observe_field(“address_country”,
:frequency => 0.25,
:update => “address_state_container”,
:url => {:controller => ‘state’, :action =>
:filtered_state_select},
:with => “‘country_code=’ + value”)
%>
StateController#filtered_state_select
def filtered_state_select #ajax action
country = Country.find_by_code(@params[“country_code”])
@state_groups = StateGroup.find_all_by_country_id(country.id)
@state_groups.each {|g| g.name = ‘- ’ + g.name + ’ -’}
render(:partial => 'options')
end
_options.rhtml partial that gets rendered:
– Select State/Province –
<%=
option_groups_from_collection_for_select(@state_groups,
:states, :name, # <- groups
:code, :name, nil) # <- items
%>
contents of @params on form submit
Parameters: {“user”=>{“name”=>"", “fax_number”=>"", “company_name”=>"",
“password_confirmation”=>"", “phone_number”=>"", “password”=>"",
“email”=>""},
“x”=>“48”, “y”=>“10”, “action”=>“create_account”,
“controller”=>“breeder”,
“address”=>{“city”=>"", “postal_code”=>"", “country”=>“US”,
“street_ext”=>"",
“street”=>""}}