rails 3 json编码以便在javascript中使用

时间:2011-03-16 17:15:13

标签: ruby-on-rails json ruby-on-rails-3

我知道to_json已被弃用,as_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) %>)

有什么想法吗?

1 个答案:

答案 0 :(得分: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}})) %>)