2013-04-15 1 views
2

예를 탈출 내가 레일 콘솔에서이 일을하고있다 :루비/레일 넷 :: HTTP.post_form 중첩 된 해시 항목 HTML을

Started POST "/download.pdf" for 127.0.0.1 at 2013-04-15 16:25:36 +0200 
Processing by PublicController#show_pdf as */* 
    Parameters: {"type"=>"raka", "fields"=>"t4", "railing"=>"A-3", "wood"=>"wood_6 
", "railing_m"=>"0", "order"=>"{\"sving\"=>\"right\", \"size\"=>{\"ground\"=>\"1 
23\", \"floor\"=>\"6\", \"a\"=>\"6\", \"d\"=>\"6\"}, \"comments\"=>{\"step_2\"=> 
\"\", \"step_3\"=>\"\", \"step_4\"=>\"\"}, \"railing\"=>{\"1\"=>\"1\", \"2\"=>\" 
1\"}, \"railing_m\"=>{\"1\"=>\"\", \"2\"=>\"\", \"3\"=>\"\", \"4\"=>\"12\"}, \"h 
ul\"=>{\"l\"=>\"123\", \"b\"=>\"123\"}, \"name\"=>\"qwed\", \"email\"=>\"[email protected]\", \"phone\"=>\"13123\", \"street\"=>\"iuuj\", \"city\"=>\"ui\", \"postn 
r\"=>\"213\"}"} 
:

params = {"type"=>["raka"], "fields"=>["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"], "railing"=>["A-3"], "wood"=>["wood_6"], "railing_m"=>"0", "order"=>{"sving"=>"right", "size"=>{"ground"=>"123", "floor"=>"6", "a"=>"6", "d"=>"6"}, "comments"=>{"step_2"=>"", "step_3"=>"", "step_4"=>""}, "railing"=>{"1"=>"1", "2"=>"1"}, "railing_m"=>{"1"=>"", "2"=>"", "3"=>"", "4"=>"12"}, "hul"=>{"l"=>"123", "b"=>"123"}, "name"=>"qwed", "email"=>"[email protected]", "phone"=>"13123", "street"=>"iuuj", "city"=>"ui", "postnr"=>"213"}} 

x = Net::HTTP.post_form(URI.parse('http://localhost:3000/download.pdf'), params) 

을 내 레일 콘솔에서 나는 HTTP POST 요청을 볼 수 있습니다

중첩 된 http 매개 변수는 모두 HTML 이스케이프 처리됩니다. 어떻게 제거 할 수 있습니까?

+0

문제를 설명해주십시오. 중첩 된 매개 변수를 이스케이프 처리하는 것이 문제가되는 경우 어떤 문제가 발생합니까? 또는 간단히 말하면, 왜 당신은 탈출을 제거하고 싶습니까? –

+0

문제는 중첩 된 매개 변수가 HTML로 이스케이프 처리된다는 것입니다. –

+1

그래서? 그것은 어쨌든 프로그램 논리에 영향을 미치나요? –

답변

1

레일즈에서 params은 Ruby가 기본적으로 제공하는 일반 Hash 객체가 아닙니다. 사실 그 키는 심볼이나 문자열로 액세스 할 수있는 레일스가 제공하는 HashWithIndifferentAccess입니다.

irb(main):001:0>params = {"type"=>["raka"], "fields"=>["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"], "railing"=>["A-3"], "wood"=>["wood_6"], "railing_m"=>"0", "order"=>{"sving"=>"right", "size"=>{"ground"=>"123", "floor"=>"6", "a"=>"6", "d"=>"6"}, "comments"=>{"step_2"=>"", "step_3"=>"", "step_4"=>""}, "railing"=>{"1"=>"1", "2"=>"1"}, "railing_m"=>{"1"=>"", "2"=>"", "3"=>"", "4"=>"12"}, "hul"=>{"l"=>"123", "b"=>"123"}, "name"=>"qwed", "email"=>"[email protected]", "phone"=>"13123", "street"=>"iuuj", "city"=>"ui", "postnr"=>"213"}} 
irb(main):002:0>params.class 
=> Hash 
irb(main):003:0>params[:fields] 
=> nil 
irb(main):004:0>params = params.with_indifferent_access 
irb(main):005:0>params.class 
=> ActiveSupport::HashWithIndifferentAccess 
irb(main):006:0>params[:fields] 
=> ["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"] 
+0

내 문제가 해결되었습니다. 이 질문에 Hallucynogenyc에 의해 anwser 사용 : http://stackoverflow.com/questions/11387760/rails-escaping-backslashes-in-params-causing-havoc –

+0

당신이 당신의 문제에 대한 답을 찾았 으면 좋겠다. –

+0

어쩌면 정상적인 해시 obj가 아니기 때문에 작동하지 않을 수도 있습니다. –

4

.post_form 메서드는 문자열을 허용하므로 중첩 해시가 전달되면 이스케이프 문제가 발생합니다. 나는이 같은 문제를 가지고 .post 방법으로 전환하여 해결했다. 또한

require "net/http" 
uri = URI('http://www.yoururl.com') 
http = Net::HTTP.new(uri.host) 
response = http.post(uri.path, params.to_query) 

참고 문자열로 변환 해시 .to_query의 방법의 용도. See here

관련 문제