2011-03-16 3 views
0

to_json이 사용되지 않으며 as_json이 문제를 일으킨다는 것을 알고 있습니다.레일즈 3 자바 스크립트에서 json 인코딩 사용

이 줄은 잘 작동하지만 to_json이되지 않습니다 :

new IS.Presentation(<%= raw(@course_step.step.step_presentation.step_presentation_files.map { |item| {'url' => item.slide.url, 'title' => item.title}}.to_json) %>) 

어떤 아이디어?

답변

0

ActiveSupport는 JSON을 지원합니다. 당신은 여기에서 볼 수 있습니다

ruby-1.9.2-p136 :003 > j = ActiveSupport::JSON 
=> ActiveSupport::JSON 
ruby-1.9.2-p136 :004 > j.encode({:team => "Celtics", :players => "20"}) 
=> "{\"team\":\"Celtics\",\"players\":\"20\"}" 
ruby-1.9.2-p136 :005 > j.decode("{\"team\":\"Celtics\",\"players\":\"20\"}") 
=> {"team"=>"Celtics", "players"=>"20"} 

그래서 당신을 위해 다음과 같습니다

new IS.Presentation(<%= ActiveSupport::JSON.encode(raw(@course_step.step.step_presentation.step_presentation_files.map { |item| {'url' => item.slide.url, 'title' => item.title}})) %>) 
+0

덕분에,이 일 : 새로운 IS.Presentation (<% = 원시 (ActiveSupport :: JSON.encode (@course_step. step.step_presentation.step_presentation_files.map {| item | { 'url'=> item.slide.url, 'title'=> item.title}})) %) –

관련 문제