[英]Ruby on Rails - Array of Hashes - Display value based on key
我有一個哈希數組保存到Rails 5 Postgres DB(Ruby 2.3.1)中。 我可以像這樣在show.html.erb
頁面上顯示此內容:
<%= @item.yearly_interest_totals %>
顯示:
[
"{:financial_year=>\"2017\", :total=>\"120.08\"}",
"{:financial_year=>\"2018\", :total=>\"237.32\"}",
"{:financial_year=>\"2019\", :total=>\"163.75\"}",
"{:financial_year=>\"2020\", :total=>\"87.95\"}",
"{:financial_year=>\"2021\", :total=>\"15.38\"}"
]
同樣在此頁面上,我有一個變量<%= fin_year %>
,顯示2017
。
我正在嘗試使用以下代碼在視圖中顯示與此fin_year
鍵對應的值,但它沒有給我提供no implicit conversion of Symbol into Integer
錯誤的no implicit conversion of Symbol into Integer
……
<%= @item.yearly_interest_totals.detect do |t|
t[:financial_year] == fin_year end [:total] %>
有人可以解釋一下為什么我收到此錯誤嗎?
更新資料
哈希鍵和局部變量都被命名為相同的名稱,這令人困惑,我將局部變量更改為fin_year
。
<%= fin_year.class%>正在生成String
<%= @ item.yearly_interest_totals.class%>正在生成Array
<%= @ item.yearly_interest_totals [0] [:financial_year] .class%>返回“符號未隱式轉換為整數的錯誤” ...
問題似乎是散列數組中的鍵:financial_year
的值是字符串(例如“ 2017”),但是變量financial_year
值卻是fixnum / integer(例如2017)。 嘗試使它們一致以進行比較,例如:
<%= @item.yearly_interest_totals.detect do |t|
t[:financial_year] == financial_year.to_s end [:total] %>
這是Rails控制台的輸出,將兩者進行了比較:
Running via Spring preloader in process 15647
Loading development environment (Rails 4.2.7.1)
2.3.3 :001 > item_yearly_interest_totals = [{ financial_year: "2017", total: "120.08" }, { financial_year: "2018", total: "237.32" }, { financial_year: "2019", total: "163.75" }, { financial_year: "2020", total: "87.95" }, { financial_year: "2021", total: "15.38" }]
=> [{:financial_year=>"2017", :total=>"120.08"}, {:financial_year=>"2018", :total=>"237.32"}, {:financial_year=>"2019", :total=>"163.75"}, {:financial_year=>"2020", :total=>"87.95"}, {:financial_year=>"2021", :total=>"15.38"}]
2.3.3 :002 > financial_year = 2017
=> 2017
2.3.3 :003 > item_yearly_interest_totals.detect do |t|
2.3.3 :004 > t[:financial_year] == financial_year end [:total]
NoMethodError: undefined method `[]' for nil:NilClass
.
.
.
2.3.3 :005 > item_yearly_interest_totals.detect do |t|
2.3.3 :006 > t[:financial_year] == financial_year.to_s end [:total]
=> "120.08"
2.3.3 :007 >
更新(02-20-2017)
我不完全了解Rails的區別在哪里或正在發生,這是問題的根源,但是即使執行@item.yearly_interest_totals[0].class
並獲得Hash
,您似乎也無法訪問使用哈希鍵的值(例如[:financial_year],[“ financial_year”]等)。
經過一番挖掘,我發現了這一點: Rails訪問哈希值和可接受的答案使我嘗試JSON.parse
,盡管使用.each
而不是.detect
,但我仍然可以使用.detect
。 這次,我在Rails 5應用程序中創建了一個Item
模型,使用了Postgres,並為單個Item
種子。 我仍然沒有做的是創建一個控制器或任何視圖。 我通過Rails控制台執行了代碼。 因此,如果您復制我的代碼但對您不起作用,則問題可能出在控制器和視圖內。
最終,關於此哈希/ JSON的區別以及實現方式如何使其表現為一個或另一個,仍有一些發現需要完成。
app / models / item.rb
class Item < ApplicationRecord
validates :name, presence: true
end
db / migrate / 20170220221004_enable_hstore_extension.rb
class EnableHstoreExtension < ActiveRecord::Migration
def change
enable_extension 'hstore'
end
end
db / migrate / 20170220221129_create_item.rb
class CreateItem < ActiveRecord::Migration[5.0]
def change
create_table :items do |t|
t.string :name, null: false, index: { unique: true }
t.hstore :yearly_interest_totals, array: true
t.timestamps null: false
end
end
end
db / seeds.rb
Item.create(name: 'Sample Item', yearly_interest_totals: [{ financial_year: "2017", total: "120.08" }, { financial_year: "2018", total: "237.32" }, { financial_year: "2019", total: "163.75" }, { financial_year: "2020", total: "87.95" }, { financial_year: "2021", total: "15.38" }])
這是在Rails控制台中執行的代碼:
Running via Spring preloader in process 19764
Loading development environment (Rails 5.0.1)
2.4.0 :001 > @item = Item.first
Item Load (1.4ms) SELECT "items".* FROM "items" ORDER BY "items"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Item id: 1, name: "Sample Item", yearly_interest_totals: [{"total"=>"120.08", "financial_year"=>"2017"}, {"total"=>"237.32", "financial_year"=>"2018"}, {"total"=>"163.75", "financial_year"=>"2019"}, {"total"=>"87.95", "financial_year"=>"2020"}, {"total"=>"15.38", "financial_year"=>"2021"}], created_at: "2017-02-20 22:25:14", updated_at: "2017-02-20 22:25:14">
2.4.0 :002 > @item.class
=> Item(id: integer, name: string, yearly_interest_totals: hstore, created_at: datetime, updated_at: datetime)
2.4.0 :003 > @item.yearly_interest_totals.class
=> Array
2.4.0 :004 > @item.yearly_interest_totals[0].class
=> Hash
2.4.0 :005 > financial_year = 2017
=> 2017
2.4.0 :006 > financial_year.class
=> Integer
2.4.0 :007 > selected_year_interest_total = nil
=> nil
2.4.0 :008 > selected_year_interest_total.class
=> NilClass
2.4.0 :009 > @item.yearly_interest_totals.each do |t|
2.4.0 :010 > puts JSON.parse(t["financial_year"]).class
2.4.0 :011 > if JSON.parse(t["financial_year"]) == financial_year
2.4.0 :012?> selected_year_interest_total = JSON.parse(t["total"])
2.4.0 :013?> end
2.4.0 :014?> end
Integer
Integer
Integer
Integer
Integer
=> [{"total"=>"120.08", "financial_year"=>"2017"}, {"total"=>"237.32", "financial_year"=>"2018"}, {"total"=>"163.75", "financial_year"=>"2019"}, {"total"=>"87.95", "financial_year"=>"2020"}, {"total"=>"15.38", "financial_year"=>"2021"}]
2.4.0 :015 > selected_year_interest_total
=> 120.08
2.4.0 :016 > selected_year_interest_total.class
=> Float
我不了解Rails 5,但也許會有所幫助,Rails 4,假設financial_year是變量,並且我正確理解了這個問題:
<% @item.yearly_interest_totals.each do |t| %>
<%= t['total'] == financial_year %>
<% end %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.