Ruby on rails 如何在rails的视图(html页面)中从json对象访问值?

Ruby on rails 如何在rails的视图(html页面)中从json对象访问值?,ruby-on-rails,json,Ruby On Rails,Json,在我的控制器类中,我从模型中得到一个json响应,我只想在视图中显示结果数组中的一个特定值。我该怎么做 在我看来,我试过了 <h4><%= @info[:result][-1] %></h4> => TypeError in Users#create no implicit conversion of Symbol into Integer <h4><%= @info['result'][-1] %></h4>

在我的控制器类中,我从模型中得到一个json响应,我只想在视图中显示结果数组中的一个特定值。我该怎么做

在我看来,我试过了

<h4><%= @info[:result][-1] %></h4>
=> TypeError in Users#create
   no implicit conversion of Symbol into Integer

<h4><%= @info['result'][-1] %></h4>
=> t 
this t is the last letter from 'result'

<h4><%= json: @info[:result][-1] %></h4>
=> Encountered a syntax error while rendering template: check <h4><%= json: @info[:result][-1] %></h4>
我的预期产出是:

{"id":"5","username":"Dev","password":"112"}
or
"id":"5","username":"Dev","password":"112"

请您在分配
@info
的位置提供一段代码,因为根据错误,它看起来像一个数组,而不是散列!您的
@info
是一个
散列
,具有
结果
作为键和数组值。您应该通过
@info[:result][0]
访问数组的第一个元素,或者如果您想
把它放出来
@info[:result].map{element | puts element}
@kunashir,我发现当我在rails中执行应该在散列上执行的操作时,即“=>”。我是在类型为“:”的json响应上做的。我所需要的只是JSON.parse(response.body,:symbol_names=>true),它就工作了。JSON.parse()将我的{“result”:[{“id”:“3”,…},{“id”:“4”,…}…}]响应转换为{:result=>[{:id=>“3”,…},{:id=>“4”,…}…}]格式,使我能够像在控制器中一样在视图中访问它。
{"id":"5","username":"Dev","password":"112"}
or
"id":"5","username":"Dev","password":"112"