kuntoaji kuntoaji - 2 months ago 25
jQuery Question

Make ajax result as select list with formtastic

is it possible to use Ajax and make the result as select list with Formtastic?


semantic_form_for @user, :url => profile_path(@profile.id) do |f|
- f.inputs do
- f.input :province, :label => "Province", :as => :select, :collection => ["province-1", "province-2", "province-3"]
- f.input :city, :label => "City", :as => :select
- f.buttons do
= f.submit "Submit"

I want to make city's select list change dynamically based on province.


If you're using Protoype, what you described can be done with a few lines of Javascript and the Prototype Ajax.Updater.

First make sure to pass Formtastic the argument :include_blank => true at the end of the f.input :collection => [] line, so that Formtastic knows to render an empty select drop-down. Then just make your controllers return the options, and in your Haml:

    new Ajax.Updater({ success: 'some_select_tag' }, '/foo', {
        // process/insert returned html options tags/data however you want
        insertion: 'bottom'