在Rails中发生未知302重定向

时间:2016-10-27 00:01:26

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

我在尝试访问某些network_hosts#show页面时正在处理302重定向。它并没有发生在所有这些上。我一直试图在过去的5个小时内诊断出这个问题并且一直困惑。

这是怎么回事......

用户点击了视图network_host/index.html.erb中的以下链接:

<%= link_to '<i class="fa fa-eye"></i>'.html_safe, network_host_path(h.id), "data-toggle" => "tooltip", "title" => "View" %>

控制器network_host#show开始了:

class NetworkHostsController < ApplicationController
  before_action :get_company_and_locations

  def show
    @network_host = NetworkHost.find(params[:id])
    if @network_host
      @major_issues = get_host_issues(@network_host, @network_host.last_test, "major")
      @minor_issues = get_host_issues(@network_host, @network_host.last_test, "minor")
    end
  end
end

访问helpers/application_helper.rb中的帮助方法:

def get_company_and_locations
  @company = current_user.company
  @devices =  Device.where(company_id: @company.id).order(name: :asc)
  @locations = if @company
    current_user.company_locations.order(:name)
  else
    []
  end
end


def get_host_issues(network_host, last_test, severity)
  get_company_and_locations
  # the issue_ids to remove since they are deferred
  deferred_ids = []
  @company.deferred_issues.each do |d|
    if d.network_host_id == network_host.id && d.expires_at.future?
      deferred_ids.push(d.issue_id)
    end
  end
  # figure out the issues
  results = last_test.results.select{|result| result.issue.severity == "#{severity}"}.collect{|issue| issue }
  issues = results.select{ |issue| issue.issue_id 
end

此时,应显示network_hosts/show.html.erb视图,但我的日志显示为302 Redirect

I, [2016-10-26T18:47:52.347035 #31947]  INFO -- : Started GET "/network_hosts/4673" for 12.33.233.231 at 2016-10-26 18:47:52 -0500
I, [2016-10-26T18:47:52.349154 #31947]  INFO -- : Processing by NetworkHostsController#show as HTML
I, [2016-10-26T18:47:52.349218 #31947]  INFO -- :   Parameters: {"id"=>"4673"}
I, [2016-10-26T18:47:52.369483 #31947]  INFO -- :   Rendered layouts/_back_button.html.erb (0.4ms)
I, [2016-10-26T18:47:52.377738 #31947]  INFO -- :   Rendered network_hosts/show.html.erb within layouts/application (10.2ms)
I, [2016-10-26T18:47:52.378656 #31947]  INFO -- : Redirected to https://www.myserver.com/
I, [2016-10-26T18:47:52.378816 #31947]  INFO -- : Completed 302 Found in 29ms (ActiveRecord: 9.7ms)

所以,在这一点上,我认为没有任何理由会重定向到我的root_url,对吗?

我唯一能够区分的是network_hosts#show在没有“次要”问题时显示(因此1个主要/ 1个小问题有效,或2个主要/ 0个小问题),但是当有0个主要和X个小问题时似乎不起作用。这导致我回到get_host_issues中的application_helper#get_host_issues功能,但是从这里开始我被卡住了。

以下是network_host/show.html.erb(已修剪):

<div class="table-responsive">
  <table id="major_issue_table" class="table table-striped">
    <thead>
     <tr>
       <th>Severity </th>
       <th>Code </th>
       <th>Problem </th>
       <th><span class="nobr">Action</span></th>
     </tr>
    </thead>
    <tbody>
      <% @major_issues.to_enum.with_index(1).each do |result, index| %>
        <% issue = result.issue %>
          <tr>
             <td><%= issue_severity(issue) %></td>
             <td><%= issue.name %></td>
             <td><%= truncate(issue.problem, length: 100) %></td>
             <td>
                <a href='#' class='deferIssue' data-toggle='modal' data-tooltip='tooltip' data-resultid='<%= result.id %>' data-issueid='<%= issue.id %>' data-target='#deferIssueModal' title='Defer'><i class='fa fa-close'></i></a>
                <%= link_to '<i class="fa fa-eye"></i>'.html_safe, issue_path({id: issue.id, network_host: @network_host.id}), "data-toggle" => "tooltip", "title" => "View" %>
             </td>
          </tr>
      <% end %>
    </tbody>
  </table>
</div>

<div class="x_content">
   <div class="table-responsive">
     <table id="minor_issue_table" class="table table-striped">
        <thead>
            <tr>
               <th>Severity </th>
               <th>Code </th>
               <th>Problem </th>
               <th><span class="nobr">Action</span></th>
             </tr>
        </thead>
        <tbody>
           <% @minor_issues.to_enum.with_index(1).each do |result, index| %>
             <% issue = result %>
             <tr>
                <td><%= issue_severity(issue) %></td>
                <td><%= issue.name %></td>
                <td><%= truncate(issue.problem, length: 100) %></td>
                <td>
                  <a href='#' class='deferIssue' data-toggle='modal' data-tooltip='tooltip' data-resultid='<%= result.id %>' data-issueid='<%= issue.id %>' data-target='#deferHostModal' title='Defer'><i class='fa fa-close'></i></a>
                  <a href="#" data-toggle="tooltip" title="Defer"><i class="fa fa-close"></i></a>
                  <%= link_to '<i class="fa fa-eye"></i>'.html_safe, issue_path(issue.id), "data-toggle" => "tooltip", "title" => "View" %>
                 </td>
              </tr>
            <% end %>
         </tbody>
     </table>
   </div>
</div>

更新:添加layouts/_back_button.html.erb

<div class="row">
  <%= link_to "<span class='btn btn-default fa fa-home' style='margin-left:10px;'></span>".html_safe, :root %>
  <%= link_to "<span class='btn btn-default fa fa-mail-reply'></span>".html_safe, :back %>
</div>

1 个答案:

答案 0 :(得分:0)

对于重大问题,你有这个:

 <% @major_issues.to_enum.with_index(1).each do |result, index| %>
    <% issue = result.issue %>
    <tr>
        <td><%= issue_severity(issue) %></td>
        ...etc...
    </tr>
<% end %>

对于小问题,你有这个:

<% @minor_issues.to_enum.with_index(1).each do |result, index| %>
    <% issue = result %>
    <tr>
        <td><%= issue_severity(issue) %></td>
        ...etc...
    </tr>
<% end %>

值得注意的是,在第一种情况下,您将Issue对象传递给issue_severity,而在第二种情况下,您传递的是Result对象。

Result对象不会响应与Issue对象相同的方法,因此issue_severity内的某个地方会出现NoMethodError。

重定向几乎肯定来自ApplicationController中的rescue_from块,它捕获控制器/视图代码中的异常并允许您执行其他的操作而不是显示通用500错误(在这种情况下,它可能类似于rescue_from(Exception) { redirect_to :root },这不是最好的计划;它应该至少在重定向之前记录异常,但是根据您发布的日志,这不会发生。