我在数据库中保存了一些JSON,我正在检索和解析但由于某种原因无法循环解析的JSON并返回它的值。
<% @submission.each do |submission| %>
<div class="field">
<p class="submissionDate">
<% @dog = JSON.parse(submission.self_interests) %> #Gets the JSON from the self_interests field
<% @dog.each do |self_interests| %>
<%= self_interests.company_name %> #trying to get the company_name from the parsed array
<% end %>
</p>
<% end %>
如果我使用<%= self_interests %>
而不是<%= self_interests.company_name %>
,它会按预期输出解析后的数组。
{"self_interest"=>{"appointment_date"=>"2012-07-19", "company_name"=>"asdasd", "company_registration"=>"asdas", "created_at"=>"2012-07-18T15:49:33+02:00", "date_deleted"=>nil, "date_registered"=>"2012-07-10", "date_terminated"=>"2012-07-27", "id"=>16, "trading_name"=>"asdasd", "transacting_with"=>1, "type_of_business"=>"asdasdasd", "updated_at"=>"2012-07-18T15:49:33+02:00", "user_id"=>2}}
对此的任何帮助都将非常感激,它一直困扰着我的大脑。我对Rails很陌生,所以它可能是我想念的东西。
答案 0 :(得分:1)
试试这个更新的
<%= self_interests["self_interest"]["company_name"] %>