出现错误时,我无法重新呈现索引页面。如果两个字段为空,则应呈现welcome / index
def index
if params[:cityzip] == "" && params[:searchname] == ""
p "***************"
@error = "You must enter a location"
p "***************"
render'welcome/index.html.erb'
elsif params[:searchname] != ""
@providers = Provider.searchname(params[:searchname].capitalize)
elsif params[:cityzip] != "" && params[:insurance][0] == "---"
@providers = Provider.search(params[:cityzip])
elsif params[:cityzip] != "" && params[:insurance][0] != ""
@providers = Provider.search(params[:cityzip]).insurancesearch(params[:insurance][0].upcase)
end
@searchname = params[:searchname]
@search = params[:cityzip]
@insurancesearch = params[:insurance]
@locations = []
@filters = []
if params[:searchname] != ""
@filters << params[:searchname]
end
if params[:insurance][0] != ""
@filters << params[:insurance][0]
end
def latlon(city)
url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + city + '&key=AIzaSyC_VdOLBtVFbR-6rRP5-hRFXChHa78bDmM'
citylatlon =[]
city = open(url).read
city = JSON.parse(city)
city["results"][0]["geometry"]["location"]
end
@providers = @providers.order("lastname")
@providers = @providers.order('distance asc').paginate(:per_page => 30, :page => params[:page])
end
索引页面位于“Views”文件夹的“Welcome”文件夹中。代码位于Providers控制器中。我有一个欢迎控制器,它有一个索引方法。这是我得到的错误:
Started GET "/providers?utf8=%E2%9C%93&insurance%5B%5D=AETNA&cityzip=&searchname=" for ::1 at 2017-06-10 12:06:04 -0500
Processing by ProvidersController#index as HTML
Parameters: {"utf8"=>"✓", "insurance"=>["AETNA"], "cityzip"=>"", "searchname"=>""}
Geokit is using the domain: localhost
Rendering welcome/index.html.erb within layouts/application
Rendered welcome/index.html.erb within layouts/application (3.4ms)
Completed 500 Internal Server Error in 23ms (Views: 20.1ms | ActiveRecord: 0.0ms)`
NoMethodError (undefined method 'order' for nil:NilClass):`
app/controllers/providers_controller.rb:36:in 'index'
Rendering /Users/dragunfly/.rvm/gems/ruby-2.4.1/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /Users/dragunfly/.rvm/gems/ruby-2.4.1/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /Users/dragunfly/.rvm/gems/ruby-2.4.1/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.7ms)
Rendering /Users/dragunfly/.rvm/gems/ruby-2.4.1/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/dragunfly/.rvm/gems/ruby-2.4.1/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.5ms)
Rendering /Users/dragunfly/.rvm/gems/ruby-2.4.1/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb`
Rendered /Users/dragunfly/.rvm/gems/ruby-2.4.1/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.5ms)
Rendered /Users/dragunfly/.rvm/gems/ruby-2.4.1/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (139.2ms)
NoMethodError (undefined method 'order' for nil:NilClass):
当它进一步向下移动控制器时会发生此错误,因此之后的任何内容都应该是无关紧要的。
layouts/application
是页面的布局,如下所示:
<!DOCTYPE html>
<html>
<head>
<title>Referralgps</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<nav class="navbar navbar-inverse nobottommargin ">
<div class="container">
<a href="/welcome/index" ><img src="/images/referral_gps_logo.png"></a>
</div>
</nav>
<%= yield %>
<footer class="visible-lg-*">
<img src="/images/footlogo.png">
</footer>
</body>
</html>
首次加载页面时,它工作正常。但是当我因为错误而尝试渲染它时它不起作用。
答案 0 :(得分:0)
在您的第一个条件中,您没有定义@providers
变量。因此,它是nil
,并且它不理解方法order
。
在向@providers
发送order
之前,至少可以检查 def index
if missing_location?
@error
else
your_main_flow
end
end
private
def missing_location?
params[:cityzip].empty? && params[:searchname].empty?
end
变量是否存在。
使事情更具可读性:
latlon(city)
同样将index
从#include "Point.hpp"
int main(){
Point p1;
}
方法的范围移到整个控制器的私有范围。