mercredi 26 octobre 2016

Unknown 302 Redirect happening in Rails

I'm dealing with a 302 Redirect when trying to access certain network_hosts#show pages. It's not happening on all of them. I've been trying to diagnose the issue for the last 5 hours and an befuddled.

Here's how it goes....

User clicks the following link in view 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" %>

The controller network_host#show kicks in:

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

Which accesses helper methods in 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

At this point, the network_hosts/show.html.erb view should show, but instead my log is showing a 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 http://ift.tt/1j3J2cD
I, [2016-10-26T18:47:52.378816 #31947]  INFO -- : Completed 302 Found in 29ms (ActiveRecord: 9.7ms)

So, at this point, I don't see any reason there would be a redirect back to my root_url, do you?

The only thing I've been able to differentiate is that the network_hosts#show displays when there are no 'minor' issues (so 1 major/1 minor issues work, or 2 major/0 minor issues), but doesn't seem to work when there are 0 major and X minor issues. Which leads me back to get_host_issues function in application_helper#get_host_issues, but from here I'm stuck.

Here is the network_host/show.html.erb (trimmed down):

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

Aucun commentaire:

Enregistrer un commentaire