Skip to content

Instantly share code, notes, and snippets.

@gudata
Created October 28, 2010 15:34
Show Gist options
  • Save gudata/651604 to your computer and use it in GitHub Desktop.
Save gudata/651604 to your computer and use it in GitHub Desktop.
In the view
class NavigationsController < ApplicationController
def cities
@cities = City.find(:all, :order => 'name')
@cities.map!{|c|
{
:id => c.id,
:name => c.name
}
}
respond_to do |format|
format.json { render :json => @cities }
end
end
end
<%= sell_search_form.input :city_id,
:label => t("Град", :scope => [:admin, :buy_search]),
:as => :select,
:collection => []
%>
<% content_for :head do %>
<script type="text/javascript">
function fill_from_storage(target_select, storage_name, data_url, value) {
// localStorage.removeItem("cities_storage" + "_options")
// localStorage.removeItem("cities_storage")
var json_stored_data = localStorage.getItem(storage_name);
if (json_stored_data == null) {
$.getJSON(data_url, function(data) {
var select_options = '';
// use http://code.google.com/p/jquery-json/
var encoded = $.toJSON(data);
localStorage.setItem(storage_name, encoded); // store the raw data
// Store parsed options tags
$(data).each(function(index, object) {
select_options += '<option value="' + object.id + '">' + object.name + '</option>';
});
localStorage.setItem(storage_name + "_options", select_options); // store the raw data
$(target_select).html(localStorage.getItem(storage_name + "_options"));
$(target_select).val(value);
});
} else {
// we don't need the raw data'
// data = localStorage.getItem(storage_name);
// items = jQuery.parseJSON(data);
$(target_select).html(localStorage.getItem(storage_name + "_options"));
$(target_select).val(value);
}
}
$(document).ready(function(){
fill_from_storage("#sell_search_city_id", "cities_storage", "<%= cities_navigations_path %>", <%= sell_search_form.object.city_id %>);
});
</script>
<% end%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment