发生在 Rails 的未知 302 重定向
Unknown 302 Redirect happening in Rails
我在尝试访问某些 network_hosts#show
页面时遇到了 302 重定向。这并没有发生在所有人身上。在过去的 5 个小时里,我一直在尝试诊断这个问题,但一头雾水。
事情是这样的....
用户在视图 network_host/index.html.erb
中单击以下 link:
<%= 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
在没有 'minor' 问题时显示(所以 1 major/1 小问题有效,或者 2 major/0 次要问题),但当存在 0 个主要问题和 X 个次要问题时似乎不起作用。这使我回到 application_helper#get_host_issues
中的 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>
对于重大问题,您有:
<% @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
块,它捕获 controller/view 代码中的异常并允许您做一些事情 other 而不是显示一般的 500 错误(在这种情况下,它可能类似于 rescue_from(Exception) { redirect_to :root }
,这不是最好的计划;它至少应该在重定向之前记录异常,但考虑到您发布的日志,那不会发生。
我在尝试访问某些 network_hosts#show
页面时遇到了 302 重定向。这并没有发生在所有人身上。在过去的 5 个小时里,我一直在尝试诊断这个问题,但一头雾水。
事情是这样的....
用户在视图 network_host/index.html.erb
中单击以下 link:
<%= 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
在没有 'minor' 问题时显示(所以 1 major/1 小问题有效,或者 2 major/0 次要问题),但当存在 0 个主要问题和 X 个次要问题时似乎不起作用。这使我回到 application_helper#get_host_issues
中的 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>
对于重大问题,您有:
<% @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
块,它捕获 controller/view 代码中的异常并允许您做一些事情 other 而不是显示一般的 500 错误(在这种情况下,它可能类似于 rescue_from(Exception) { redirect_to :root }
,这不是最好的计划;它至少应该在重定向之前记录异常,但考虑到您发布的日志,那不会发生。