2015-03-13 53 views
1

雖然傳遞一個特殊字符的字符串,我得到無效的請求:無效的HTTP格式,解析失敗錯誤。日誌中的錯誤如下。無效的請求:無效的HTTP格式,在Rails解析失敗

我的要求:

http://localhost:3000/search/% 

錯誤日誌:

Invalid request: Invalid HTTP format, parsing fails. 
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/request.rb:84:in `execute' 
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/request.rb:84:in `parse' 
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/connection.rb:39:in `receive_data' 
/.rvm/gems/ruby-1.9.3-p545/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine' 
/.rvm/gems/ruby-1.9.3-p545/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run' 
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/backends/base.rb:73:in `start' 
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/server.rb:162:in `start' 
/.rvm/gems/ruby-1.9.3-p545/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run' 
/.rvm/gems/ruby-1.9.3-p545/gems/rack-1.5.2/lib/rack/server.rb:264:in `start' 
/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands/server.rb:84:in `start' 
/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:76:in `block in <top (required)>' 
/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:71:in `tap' 
/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:71:in `<top (required)>' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 

可能是什麼問題呢?請給我一個解決這個問題的想法。

如何重定向到另一個頁面,同時得到以下錯誤?

enter image description here

+0

你應該逃避百分比符號的URL。 – 2015-03-13 11:53:54

+0

如果用戶輸入url,我需要重定向到任何錯誤頁面,該怎麼做? – 2015-03-13 12:37:25

+0

根據錯誤,您可以重定向到某個頁面。假設'404'沒有找到頁面錯誤,那麼在路由中你可以'匹配'/ 404',到''controller_name#page_not_found',通過::all'並創建一個名爲'page_not_found'的頁面,並且在那個控制器中的方法' page_not_found' – Sontya 2015-03-13 12:43:59

回答

1

如下更新您的nginx配置文件,並在公用文件夾中創建400.html文件。

服務器 收聽80;

root /public; # <--- be sure to point to 'public'! 

error_page 400 /400.html; 
location = /400.html { 
    internal; 
} 

}

0

有其上升爲處理此類異常的數錯誤。 like ActiveRecord::RecordInvalidActiveRecord::RecordNotFound。但這是模型例外。 帶控制器,我們有ActionController::RoutingErrorActionController::BadRequest這是有關路由,映射URL

因此,當你400錯誤這是一個bad request error,所以你必須與調用,將響應渲染HTML頁面的方法來處理它

試試這個

class ApplicationController < ActionController::Base 
    rescue_from ActionController::RoutingError, :with => :route_not_found_error 
    rescue_from ActionController::BadRequest, :with => :bad_request_error 
    resue_from StandardError, :with => :render_server_error 

    protected 
     def route_not_found_error 
      render "shared/404", :status => 404 
     end 

     def bad_request_error 
      render "shared/400", :status => 400 
     end 

     def render_server_error 
      render "shared/500", :status => 500 
     end 
end 

頁面添加您404.html400.html500.html在app /視圖/共享

+0

它不起作用 – 2015-03-16 05:28:00

+0

還是一樣的錯誤? – Sontya 2015-03-16 06:51:26

+0

是@Sontya。首先,控制權不會進入我們的路線本身。我猜這個問題很薄。 – 2015-03-16 06:55:24