Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Ajax refresh while persisting params

shakycode asked in Javascript

I'm maintaining an old Rails app and am trying to have a my index action use a form_tag to pass the params of a region so I can scope the results of calls by region. This works fine and dandy, but I need to figure out how to do an ajax refresh of the partials while persisting the params that come from the view.

Here's some code

index action on calls controller

  def index
    if params[:region].present?
      @assigned = Call.where(region_id: params[:region][:area]).assigned_calls.until_end_of_day
      @unassigned = Call.where(region_id: params[:region][:area]).unassigned_calls.until_end_of_day
      @note = Note.new
    else
      @assigned = Call.assigned_calls.until_end_of_day
      @unassigned = Call.unassigned_calls.until_end_of_day
     @note = Note.new
   end
  end

reload actions on calls controller

def reload_active
    if params[:region].present?
      @assigned = Call.where(region_id: params[:region][:area]).assigned_calls.until_end_of_day
      @unassigned = Call.where(region_id: params[:region][:area]).unassigned_calls.until_end_of_day
      @note = Note.new
    else
      @assigned = Call.assigned_calls.until_end_of_day
      @unassigned = Call.unassigned_calls.until_end_of_day
     @note = Note.new
   end
    render :partial => "calls/assigned_calls"
  end

  def reload_inactive
    if params[:region].present?
      @assigned = Call.where(region_id: params[:region][:area]).assigned_calls.until_end_of_day
      @unassigned = Call.where(region_id: params[:region][:area]).unassigned_calls.until_end_of_day
      @note = Note.new
    else
      @assigned = Call.assigned_calls.until_end_of_day
      @unassigned = Call.unassigned_calls.until_end_of_day
     @note = Note.new
   end
    render :partial => "calls/unassigned_calls"
  end

index.html.erb

<div id="active">
  <%= render "assigned_calls" %>
</div>

<div id="inactive">
  <%= render "unassigned_calls" >
</div>


<script>
$(document).ready(
         function() {
          setInterval(function() {
            $('#active').load('/calls/reload_active');
            $('#inactive').load('/calls/reload_inactive');
        }, 30000);
    });
</script>

routes.rb

resources :calls do
    collection do
      get 'reload_active'
      get 'reload_inactive'
    end
end

_assigned_calls.html.erb (excerpt of form_tag

 <%= form_tag calls_path, :method => 'get' do %>
 <%= select_tag "region[area]", options_from_collection_for_select(Region.order(:area), :id, :area, selected: params[:region].try(:[], :area)), prompt: "Choose Region" %>
 <%= submit_tag "Select", :name => nil, :class => 'btn' %>

So when I do an initial page load of /calls and then select the region and choose submit/select it will pass the params and load the calls by the region no problem.

The problem springs up when I'm doing the ajax refresh, it will not persist the params from the URL and I'm stumbling on how to pass them. The partials should refresh every 30 seconds and load call data scoped by region where the params were previously passed.

I'm doing some googling and stack review but figured I'd open the question here if anyone had any advice on how to do this. I'm sure it's possible but to be honest my JS/jQuery game is a bit weak.

Thanks in advance guys!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.