私はjsonから値を抽出しようとしているRailsの初心者です。ビューのコードは次のようになります。
<% companyarray = @companies.select do |hash|
hash.id == acquisition.acquirer_id
end %>
<%= JSON.parse(companyarray.to_json)[0]["name"] %>
companyarray で to_json を使用する前に取得するデータセットは次のようになります。
[#<Company id: 504, name: "test", website: "http://info.test.com/", created_at: "2013-08-09 13:27:16", updated_at: "2013-08-09 13:27:16", base_id: nil>]
結局のところ、ハッシュの配列からキー値を抽出できません。undefined method []' for nil:NilClass
エラーになってしまいました。
これは、私のRailsコントローラがどのように見えるかです:
class AcquisitionsController < ApplicationController
def index
@acquisitions = Acquisition.all
@companies = Company.all
respond_to do |format|
format.html
format.json
end
end
end
何が問題だったのかわかりません。