渲染文本在ror中转换字符串

时间:2015-08-04 17:57:46

标签: ruby-on-rails json render

我在没有对象标题的情况下渲染json。所以我尝试将其呈现为文本如下

 render :text=>
          [{
          "id": "0",
          "heading": "Trending this Week",
          "title": "Prost Brew Pub",
          "place": "Sholinganallur",
          "image": "http://192.168.0.103/test2/blur.jpg",
          "rating": 4.3,
          "amount": "Rs. 1800 for two people",
          "review": "320 Reviews",
          "photos": "630 Photos",
          "genre": ["British", "Mexican", "Continental"]
},...

将上述内容转换为以下内容

[{:id=>"0", :heading=>"Trending this Week", :title=>"Prost Brew Pub", :place=>"Sholinganallur", :image=>"http://192.168.0.103/test2/blur.jpg", :rating=>4.3, :amount=>"Rs. 1800 for two people", :review=>"320 Reviews", :photos=>"630 Photos", :genre=>["British", "Mexican", "Continental"]}

为什么将"id": "0"转换为:id=>"0"

如何在不做任何更改的情况下渲染上面的数组?提前谢谢。

是否有可用的渲染方法来获取没有它的对象的标题?

1 个答案:

答案 0 :(得分:1)

你的“:text”是一个Hashes数组。所以Ruby用Hash语法渲染它。如果你想要json,你应该呈现json而不是text

render json: @object, status: :ok