【问题标题】:Filtering by params works in development but not in production按参数过滤在开发中有效,但在生产中无效
【发布时间】:2016-07-11 23:02:10
【问题描述】:

我有一个跟踪调度调用的 Rails 3.2.22 应用程序,每个调用属于一个区域,一个区域有很多调用。在我看来,我最初有来自所有区域的所有调用,但现在我在视图中使用简单的 form_tag 按区域进行过滤,并将区域 ID 作为参数传递给控制器​​并返回给视图。

因此,如果我在本地点击调用索引视图,我将触发如下网址:

http://loopify.xyz:9001/calls?utf8=%E2%9C%93&region=1

在我看来,这将显示所有区域 ID 为“1”的呼叫。我可以在视图中切换到不同的区域,它会按区域显示正确过滤的调用。

当我将它部署到我的登台服务器时,参数过滤根本不起作用并显示所有调用,即使当我选择一个区域时,我会得到如下 URL:

http://staging.example.com/calls?utf8=%E2%9C%93&region=1

这是我的呼叫控制器的索引操作:

  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

这是我的 index.html.erb 视图:

<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>

这里是允许ajax刷新和JS的index.js.erb文件

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

这里是分配部分的摘录。已分配和未分配都非常大,可以在这里完整显示,但如果您需要更多上下文,我们很乐意在 github gist 中提供它们。

<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 %>

我真的不确定这里有什么问题。在开发中我使用 Thin 为应用程序提供服务,在登台时我使用乘客和 nginx。

我的代码中是否有某些内容无法在我缺少的生产环境中运行?

回顾一下,区域过滤在开发中完美运行,但一旦部署到暂存过滤就不起作用了。

我挖掘了日志,这是来自 dev(本地)和 prod(暂存)日志的请求:

开发者:

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

产品:

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

【问题讨论】:

  • 您是否检查过数据库中包含无效数据或类似情况的记录?打开 rails c 并尝试按区域 id 获取所有调用,看看它是否与您认为代码正在执行的操作相匹配。比较您的开发机器和暂存机器上的结果。这是我唯一能想到的。
  • @MurifoX 这是我检查的第一件事,但事情就是这样。我的暂存数据库是生产的实时副本,它也在我的开发机器上复制。所以我认为这不是数据问题。这就像它忽略了所有的参数过滤,即使请求是正确的。

标签: ruby-on-rails ruby-on-rails-3 params


【解决方案1】:

我不确定为什么会发生/起作用,但确实如此。如果我将 production.rb 中的日志级别从 info 转到调试参数过滤工作正常。我在一个旧的 github 问题上看到了对此的引用。 Rails 核心将其作为非错误关闭,但显然这有一些问题。不幸的是,不再支持 3.2,所以这个“hack”将不得不做。现在是时候将应用升级到 4.2.6。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    相关资源
    最近更新 更多