问题
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