lundi 11 juillet 2016

Filtering by params works in development but not in production

I have a Rails 3.2.22 app that tracks dispatch calls and each call belongs to a region and a region has many calls. In my views I originally had all calls from all regions, but now I'm filtering by regions using a simple form_tag in the view and passing the region ID as a param to the controller and back to the view.

So locally if I hit the calls index view I will trigger a url like:

http://ift.tt/2a3IsxB

Which will then in my views show me all calls with the region id of "1". I can switch to different regions in the views and it will display the proper filtered calls by region.

When I deployed this to my staging server, the params filtering does not work at all and shows all calls even though when I select a region I will get a URL like:

http://ift.tt/29zWisT

Here is the index action of my calls controller:

  def index
   if params[:region].present?
     @region = params[:region]
     @assigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).assigned_calls.until_end_of_day
     @unassigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).unassigned_calls.until_end_of_day
   else
     params[:region] = "1"
     @assigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).assigned_calls.until_end_of_day
     @unassigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).unassigned_calls.until_end_of_day
   end
    @note = Note.new
    @units = Unit.active.order("unit_name").map{|unit| unit.calls.where(call_status: "open").empty? ? ["#{unit.unit_name} #{unit.unit_type.unit_type} #{unit.status.unit_status}", unit.id] : ["#{unit.unit_name} (on call) #{unit.unit_type.unit_type} #{unit.status.unit_status}", unit.id] }
  end

Here is my index.html.erb view:

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

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


<script>
  $(function() {
    setInterval(function(){
      $.getScript('/calls/?region=<%= params[:region] %>')
    }, 20000);
  });
</script>

Here is the index.js.erb file to allow ajax refresh and JS

$("#active").html("<%= escape_javascript render("assigned_calls") %>");
$("#inactive").html("<%= escape_javascript render("unassigned_calls") %>");

Here is an excerpt of the assigned partial. Both assigned and unassigned really large to display here in their entirety but will be happy to supply them in a github gist if you need more context.

<div class="page-header well">
  <h3><%= pluralize(@assigned.size, "Active Call") %></h3>
</div>
<%= form_tag calls_path, :method => 'get' do %>
  <%= select_tag "region", options_from_collection_for_select(Region.order(:area), :id, :area, selected: @region), prompt: "Choose Region" %>
  <%= submit_tag "Select", :name => nil, :class => 'btn' %>
<% end %>
<% @assigned.each_with_index do |call, index| %>
  <div class="widget">
    <div class="widget-header">
      <div class="pull-right">

        <%= link_to 'View', call, :class => 'btn btn-primary btn-small'%>
        <% if dispatch? || admin? || manager? || operations? %>
        <%= link_to 'Edit', edit_call_path(call), :class => 'btn btn-info btn-small'%>
        <%= link_to "Close", '#close-modal', data: {toggle: "modal", target: "#close-modal#{index}" }, class: 'btn btn-small btn-warning' %>
        <%= render 'layouts/close_call_modal', call: call, index: index %>
        <%= link_to "Cancel", '#cancel-modal', data: {toggle: "modal", target: "#cancel-modal#{index}" }, class: 'btn btn-small btn-danger' %>
        <%= render 'layouts/cancel_call_modal', call: call, index: index %>
        <%= link_to "Notes", '#note-modal', data: {toggle: "modal", target: "#note-modal#{index}" }, class: 'btn btn-small btn-primary' %>
        <%= render 'layouts/note_modal', call: call, index: index %>
        <% end %>
      </div>
      <i class="icon-phone"></i>
      <h3><%= link_to call.incident_number, call %> <span><%= status(call) %></span></h3>
      <% if call.wait_return == "yes" && call.parent_call_id == nil %>
        <i class="icon-arrow-right dim"></i> <span class="badge badge-important" >Initial Transport</span>
      <% end %>
      <% if call.wait_return == "yes" && call.parent_call_id.present? %>
        <i class="icon-arrow-left dim"></i> <span class="badge badge-important" >Return Transport</span>
      <% end %>
      <% if call.traffic_type == "Emergency" %>
      <span class="badge badge-important"><%= call.traffic_type %></span>
      <% else %>
      <span class="badge badge-info"><%= call.traffic_type %></span>
     <% end %>
     <span class="badge badge-primary" ><%= call.region.try(:area) %></span>
     <span class="badge badge-info" ><%= call.service_level.try(:level_of_service) %></span>
    </div>
 <% end %>

I'm really not sure what the problem is here. In development I'm serving the app with Thin, in staging I'm using passenger and nginx.

Is there something in my code that will not work in a production environment that I'm missing?

To recap, the region filtering works perfectly in development but once deployed to staging the filtering does not work.

I dug into the logs and here is the request from both dev (local) and prod (staging) logs:

dev:

Started GET "/calls/?region=1&_=1468278029235" for 127.0.0.1 at 2016-07-11 18:00:29 -0500
Processing by CallsController#index as JS

prod:

Started GET "/calls/?region=1&_=1468278074295" for 192.168.130.1 at 2016-07-11 18:01:14 -0500
Processing by CallsController#index as JS

Aucun commentaire:

Enregistrer un commentaire