I have a Ruby on Rails app with some jquery to render partials asynchroneously. So first I want to change to active state of the tabs, then clear the tab pages and then load the partial:
$('#availability').addClass('active');
$('#overview').removeClass('active');
$('#availabilityTab').addClass('active').addClass('in');
$('#overviewTab').removeClass('active').removeClass('in');
$("#availabilityTab").html("<%= escape_javascript(render(partial: 'availability')) %>");
This works fine, however when I click the tab to trigger this, I expect to see a blank tabpage first and the the loaded data. What happens is when the tab is clicked, nothing seems to happen and then after 3 seconds the partials shows up. This is because the database call takes a while (needs to be optimized :-) ). This is confusing for the user, I rather want to see an empty tab page first. How can I do this?
Update: This is how the controller action looks like:
def index
@customers = Customer.order(:name)
@target = params[:target] unless params[:target].blank?
respond_to do |format|
format.html
format.js
end
end
And this is index.js.erb
:
<% if @target == "overview" -%>
$('#availability').removeClass('active');
$('#overview').addClass('active');
$('#overviewTab').addClass('active').addClass('in');
$('#availabilityTab').removeClass('active').removeClass('in');
$("#overviewTab").empty().html("<%= escape_javascript(render(partial: 'overview')) %>");
<% elsif @target == "availability" -%>
$('#availability').addClass('active');
$('#overview').removeClass('active');
$('#availabilityTab').addClass('active').addClass('in');
$('#overviewTab').removeClass('active').removeClass('in');
$("#availabilityTab").html("<%= escape_javascript(render(partial: 'availability')) %>");
<% end %>