how to parse and display hstore key/value in rails

情到浓时终转凉″ 提交于 2019-12-11 18:14:17

问题


I'ved got a data type that is postgres hstore that stores a json string.

I want to be able to parse and display the key/values on rails html page.

Somehow, I just do not know how to run a parse on the data field and display each one of key/value listed in the string.

<% @payment_accounts.each do |payment_account| %>
  <tr>

    <td><%= payment_account.name %></td>
    <td><%= payment_account.company %></td>
    <td><%= payment_account.data %></td>        <-- this is the hstore json string
     <td><%= Json.parse(payment_account.data) %></td>   <-- this is an error, just to show

  </tr>
<% end %>

example would be payment_account.data contains {"hello"=>"world", "great"=>"job"}

here is the index.html.erb code. https://gist.github.com/axilaris/9174206

what should i do to accomplish this ? that is parsing hstore string to display a query result in rails ?


回答1:


You can access the data like an array:

<%= payment_account.data['hello'] %>

That will display world



来源:https://stackoverflow.com/questions/21971633/how-to-parse-and-display-hstore-key-value-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!