Rails [有偿求助] Rails MissingTemplate error

seabornlee · 2016年02月19日 · 最后由 seabornlee 回复于 2016年02月19日 · 4409 次阅读

每天都会收到多次如下的异常通知:

An ActionView::MissingTemplate occurred in pages#home:

 Missing template pages/home, application/home with {:locale=>[:zh], :formats=>["*/*;"], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :arb, :haml, :jbuilder]}. Searched in:
 * "/home/railsu/shenzhenware/releases/1280/app/views"
 * "/home/railsu/shenzhenware/shared/bundle/ruby/2.2.0/gems/mailboxer-0.12.5/app/views"
 * "/home/railsu/shenzhenware/shared/bundle/ruby/2.2.0/gems/china_city-0.0.4/app/views"
 * "/home/railsu/shenzhenware/shared/bundle/ruby/2.2.0/bundler/gems/activeadmin-7aef260921d4/app/views"
 * "/home/railsu/shenzhenware/shared/bundle/ruby/2.2.0/gems/kaminari-0.16.1/app/views"
 * "/home/railsu/shenzhenware/shared/bundle/ruby/2.2.0/gems/devise_invitable-1.3.6/app/views"
 * "/home/railsu/shenzhenware/shared/bundle/ruby/2.2.0/gems/devise-3.4.1/app/views"




-------------------------------
Request:
-------------------------------

 * URL        : http://www.shenzhenware.com/
 * HTTP Method: GET
 * IP address : 218.17.230.230
 * Parameters : {"controller"=>"pages", "action"=>"home"}
 * Timestamp  : 2016-02-18 10:03:24 +0800
 * Server : iZ947qrvfrmZ
 * Rails root : /home/railsu/shenzhenware/releases/1280
 * Process: 24916

-------------------------------
Session:
-------------------------------

 * session id: "039a91ab3ec91c8de4b616caa1ab79ab"
 * data: #<ActionDispatch::Request::Session:0x00000005ba39c0 ...>

-------------------------------
Environment:
-------------------------------

 * HTTP_ACCEPT                                    : */*;
 * HTTP_ACCEPT_LANGUAGE                           : zh-CN,zh;q=0.8,en;q=0.6
 * HTTP_CACHE_CONTROL                             : max-age=0
 * HTTP_HOST                                      : www.shenzhenware.com
 * HTTP_USER_AGENT                                : Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; BIDUBrowser 7.6)
 * HTTP_VERSION                                   : HTTP/1.1
 * ORIGINAL_FULLPATH                              : /
 * ORIGINAL_SCRIPT_NAME                           : 
 * PASSENGER_CONNECT_PASSWORD                     : [FILTERED]
 * PATH_INFO                                      : /
 * QUERY_STRING                                   : 
 * REMOTE_ADDR                                    : 218.17.230.230
 * REMOTE_PORT                                    : 12832
 * REQUEST_METHOD                                 : GET
 * REQUEST_URI                                    : /
 * ROUTES_11006260_SCRIPT_NAME                    : 
 * SCRIPT_NAME                                    : 
 * SERVER_NAME                                    : www.shenzhenware.com
 * SERVER_PORT                                    : 80
 * SERVER_PROTOCOL                                : HTTP/1.1
 * SERVER_SOFTWARE                                : nginx/1.8.0 Phusion_Passenger/5.0.20
 * action_controller.instance                     : #<PagesController:0x00000005b9e380>
 * action_dispatch.backtrace_cleaner              : #<Rails::BacktraceCleaner:0x000000024025c0>
 * action_dispatch.cookies                        : #<ActionDispatch::Cookies::CookieJar:0x00000005b91db0>
 * action_dispatch.cookies_digest                 : 
 * action_dispatch.cookies_serializer             : json
 * action_dispatch.encrypted_cookie_salt          : encrypted cookie
 * action_dispatch.encrypted_signed_cookie_salt   : signed encrypted cookie
 * action_dispatch.http_auth_salt                 : http authentication
 * action_dispatch.key_generator                  : #<ActiveSupport::CachingKeyGenerator:0x00000003cb76d8>
 * action_dispatch.logger                         : #<ActiveSupport::Logger:0x00000003930e88>
 * action_dispatch.parameter_filter               : [:password]
 * action_dispatch.redirect_filter                : []
 * action_dispatch.remote_ip                      : 218.17.230.230
 * action_dispatch.request.accepts                : [#<Mime::Type:0x00000005b9c288 @synonyms=[], @symbol=nil, @string="*/*;">]
 * action_dispatch.request.content_type           : 
 * action_dispatch.request.formats                : [#<Mime::Type:0x00000005b9c288 @synonyms=[], @symbol=nil, @string="*/*;">]
 * action_dispatch.request.parameters             : {"controller"=>"pages", "action"=>"home"}
 * action_dispatch.request.path_parameters        : {:controller=>"pages", :action=>"home"}
 * action_dispatch.request.query_parameters       : {}
 * action_dispatch.request.request_parameters     : {}
 * action_dispatch.request.unsigned_session_cookie: {"session_id"=>"039a91ab3ec91c8de4b616caa1ab79ab"}
 * action_dispatch.request_id                     : f7b35495-05b9-4f05-a92c-1e4a04252911
 * action_dispatch.routes                         : #<ActionDispatch::Routing::RouteSet:0x000000014fe268>
 * action_dispatch.secret_key_base                : ***
 * action_dispatch.secret_token                   : 
 * action_dispatch.show_detailed_exceptions       : false
 * action_dispatch.show_exceptions                : true
 * action_dispatch.signed_cookie_salt             : signed cookie
 * newrelic.transaction_started                   : true
 * rack.errors                                    : #<IO:0x0000000067d4f0>
 * rack.hijack                                    : #<Proc:[email protected]/home/railsu/.rvm/gems/ruby-2.2.3/gems/passenger-5.0.20/src/ruby_supportlib/phusion_passenger/rack/thread_handler_extension.rb:81 (lambda)>
 * rack.hijack?                                   : true
 * rack.input                                     : #<PhusionPassenger::Utils::TeeInput:0x00000005bc4c60>
 * rack.multiprocess                              : true
 * rack.multithread                               : false
 * rack.request.cookie_hash                       : {}
 * rack.request.query_hash                        : {}
 * rack.request.query_string                      : 
 * rack.run_once                                  : false
 * rack.session                                   : #<ActionDispatch::Request::Session:0x00000005ba39c0>
 * rack.session.options                           : #<ActionDispatch::Request::Session::Options:0x00000005ba3920>
 * rack.url_scheme                                : http
 * rack.version                                   : [1, 2]
 * warden                                         : Warden::Proxy:48043960 @config={:default_scope=>:user, :scope_defaults=>{}, :default_strategies=>{:user=>[:rememberable, :database_authenticatable]}, :intercept_401=>false, :failure_app=>#<Devise::Delegator:0x00000006b19530>}

尝试过升级 Rails 版本,没有解决。 已困扰多时,高手给看看,若能解决问题,重谢!

/home/railsu/shenzhenware/releases/1280/app/views/pages/home.html.erb 这个路径下面的文件不存在了?

注意 respond_to 这里的顺序

respond_to do |format|
  format.js
  format.html
end

像上面这种写法,curl 或爬虫 (对应 :formats=>["*/*;"]) 则会报上述错误...要优先把 format.html 放在前面

另外一种解法 format.all

respond_to do |format|
  format.all { render :show, formats: [:html]}
end

Rei 真是好人。

#3 楼 @rei 谢谢你, Rei!

#2 楼 @leekelby 看起来像百度的 spider,ip 是深圳电信。 可是我没办法重现问题,测试代码也无法重现。

@seabornlee 可能重现的方式

curl -H "Accept: */*" -X GET localhost:3000

好像影响的参数是 request.formats

这样应该就可以了

respond_to do |format|
        format.html { render  'some' }
        format.all { head(:unauthorized) }
end

#7 楼 @fate +1 #6 楼 @seabornlee 你可以检查一下,是不是没做对,所以重现不了。

解决思路有两种:1)指定请求格式 2)即使请求格式不对,也能响应 第一种,你可以用 before_action 之类的指定 request format(response format 也就对应上了) 第二种,例如我上面的示例~~

不知道其他页面有没有类似问题,你可以自己根据需求进行选择…

一般是劣质爬虫才会请求*/*;,所以我一般忽略这个错误,这里可以忽略 User Agent BIDUBrowser。不知有没有更好的处理方法?

一个方法是加个 Middleware,把 */*; 改写为 text/html, */*;,这样应用层不需要改变。

respond_to do |format|
  format.html
  format.js
  format.all { render :home, formats: [:html] }
end
describe 'GET home' do
  it 'renders html template' do
    request.accept = 'text/html'
    get :home
    expect(response).to render_template :home
    expect(response.header['Content-Type']).to include 'text/html'
  end

  it 'renders js template' do
    request.accept = 'application/javascript'
    get :home
    expect(response).to render_template :home
    expect(response.header['Content-Type']).to include 'text/javascript'
  end

  it 'renders html template for other formats' do
    request.accept = "*/*;"
    get :home
    expect(response).to render_template :home
  end
end

感谢各位支招!解决了困扰我好久的问题。 加我微信 seabornlee,给大家发红包!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号