Reputation: 6244
In my layout file I have two pull down menus. When a selection is made in one (category), the contents of the second (item) needs to be updated.
in layout file:
<div id="pull_down_items" class="pull-right">
<%= render :partial => 'layouts/pull_down_items' %>
</div>
<%= render :partial => 'layouts/pull_down_category' %>
layouts/_pull_down_items.html.erb:
<%= select_tag "current_item", options_from_collection_for_select(current_category.items, "id", "item_name", session[:current_item]), :id=>"change_current_item", :remote => true %>
layouts/_pull_down_category.html.erb:
<%= select_tag "current_category", options_from_collection_for_select(categories, "id", "category_name", session[:current_category]), :id=>"change_current_category", :remote => true %>
jQuery in application.js:
$("#change_current_category").change(function() {
var category_id = $(this).val();
var url = "/system/" + category_id + "/change_category";
$.post(url, category_id, function(html) {
});
});
In my system_controller:
def change_category
unless params["id"].blank?
session[:current_category] = params["id"].to_i
current_category.reload # this is a method in application_controller.rb
end
respond_to do |format|
format.js
end
end
in change_team.js.erb:
$("#pull_down_items").html("<%= render :partial => 'layouts/pull_down_items' %>")
Result: session[:current_category] IS changed to the correct category. The items pull down menu div is NOT reloaded.
Thanks for your help.
Upvotes: 3
Views: 228
Reputation: 172
By default, jQuery.post
doesn't guarantee to execute the content it downloads. Try setting dataType
argument to 'script', as seen on http://api.jquery.com/jQuery.post/
"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.
Upvotes: 1