2016-07-05 45 views
1

所以我有一個網站已經完全工作,在URL中編碼了一些URI。錯誤的URI Ruby on Rails:是因爲長?

然而,當我嘗試將URL傳遞給我的Chrome瀏覽器:

http://somewhere:3000/find/someOne?utf8=%E2%9C%93&search=someThing&choicen=no&querys={%22peopleName%22%3A%22%22%2C%22peopleGroup%22%3A%22%22%2C%22place%22%3A%22%22%2C%22pip%22%3A%22%22%2C%22hw%22%3A%22%22%2C%22somerock%22%3A%22%22%2C%22rocksomerock%22%3A%22%22%2C%22diedAt%22%3A%222016-01-01%20-%202016-12-31%22%2C%22borndAt%22%3A%22%22%2C%22taxRate%22%3A%22%22}

- 它拋出我在瀏覽器中的錯誤:

Bad Request 

bad URI `/find/someOne?utf8=%E2%9C%93&search=someThing&choicen=no&querys={%22peopleName%22%3A%22%22%2C%22peopleGroup%22%3A%22%22%2C%22place%22%3A%22%22%2C%22pip%22%3A%22%22%2C%22hw%22%3A%22%22%2C%22somerock%22%3A%22%22%2C%22rocksomerock%22%3A%22%22%2C%22diedAt%22%3A%222016-01-01%20-%202016-12-31%22%2C%22borndAt%22%3A%22%22%2C%22taxRate%22%3A%22%22}'. 

WEBrick/1.3.1 (Ruby/1.9.3/2014-11-13) at somewhere.com:3000 

同時顯示[2016-07-04 18:11:31] ERROR bad URI在導軌控制檯

版本:

  • Rails3中
  • 的Ruby 1.9.3

不知道如何得到它的工作?是因爲URI中的{}還是因爲它太長?

+0

我首先嚐試uuencoding的''{/'} '看看是否能解決這個問題。 –

+0

請修改您的問題以包含您的代碼。另外,請澄清你的問題。 「當我嘗試粘貼URL時」是什麼意思?將它粘貼在哪裏? –

回答

1

解析路徑中在呼入請求的控制器,使用Rack::Utils#parse_nested_query,請參見:經由控制檯http://www.rubydoc.info/github/rack/rack/master/Rack/Utils.parse_nested_query

# config/routes.rb 

get '/find/someOne/*str' => 'find#someOne' 


# app/controllers/find_controller.rb 

class FindController < ApplicationController 
    def someOne 
    custom_params = Rack::Utils.parse_nested_query(request.env['ORIGINAL_FULLPATH']) 
    querys_hash = JSON.parse(custom_params["querys"]) 
    end 
end 

實施例:

 
$ bundle exec rails c 
Running via Spring preloader in process 31944 
Loading development environment (Rails 5.0.0) 
irb(main):001:0> custom_params = Rack::Utils.parse_nested_query "utf8=%E2%9C%93&search=someThing&choicen=no&querys={%22peopleName%22%3A%22%22%2C%22peopleGroup%22%3A%22%22%2C%22place%22%3A%22%22%2C%22pip%22%3A%22%22%2C%22hw%22%3A%22%22%2C%22somerock%22%3A%22%22%2C%22rocksomerock%22%3A%22%22%2C%22diedAt%22%3A%222016-01-01%20-%202016-12-31%22%2C%22borndAt%22%3A%22%22%2C%22taxRate%22%3A%22%22}" 
=> {"utf8"=>"✓", "search"=>"someThing", "choicen"=>"no", "querys"=>"{\"peopleName\":\"\",\"peopleGroup\":\"\",\"place\":\"\",\"pip\":\"\",\"hw\":\"\",\"somerock\":\"\",\"rocksomerock\":\"\",\"diedAt\":\"2016-01-01 - 2016-12-31\",\"borndAt\":\"\",\"taxRate\":\"\"}"} 
irb(main):002:0> querys_hash = JSON.parse custom_params["querys"] 
=> {"peopleName"=>"", "peopleGroup"=>"", "place"=>"", "pip"=>"", "hw"=>"", "somerock"=>"", "rocksomerock"=>"", "diedAt"=>"2016-01-01 - 2016-12-31", "borndAt"=>"", "taxRate"=>""}