现在我正在创建一个数组并使用:
render :json => @comments
这对于一个简单的 JSON 对象来说很好,但现在我的 JSON 对象需要几个帮助程序,这会破坏一切,并且需要在 Controller 中包含帮助程序,这似乎导致的问题多于解决的问题。
那么,如何在 View 中创建这个 JSON 对象,在使用帮助器时我不必担心做任何事情或破坏任何事情。现在我在 Controller 中制作 JSON 对象的方式看起来有点像这样?帮我将它迁移到一个 View :)
# Build the JSON Search Normalized Object
@comments = Array.new
@conversation_comments.each do |comment|
@comments << {
:id => comment.id,
:level => comment.level,
:content => html_format(comment.content),
:parent_id => comment.parent_id,
:user_id => comment.user_id,
:created_at => comment.created_at
}
end
render :json => @comments
谢谢!
最佳答案
或使用:
<%= raw(@comments.to_json) %>
转义任何 html 编码字符。
关于ruby-on-rails - rails - 如何在 View 中呈现 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5161579/