我在卡片列表中遇到了一个问题。
我有个问题,上面写着3行问题:
.placeholder2.container.visible.card_list.row
- items.each_with_index do |cards, i|
.col-md.card-column{data: {color: cards[4].first.color, type: cards[4].first.card_class}}
%h4
%span{class: "icon-#{i == 4 ? ico_item : icon}"}
= entities[i]
在日志文件中显示:
ActionView::Template::Error (undefined method `first' for nil:NilClass):
我在此模板中的完整代码:
.placeholder2.container.visible.card_list.row
- items.each_with_index do |cards, i|
.col-md.card-column{data: {color: cards[1].first.color, type: cards[1].first.card_class}}
%h4
%span{class: "icon-#{i == 4 ? ico_item : icon}"}
= entities[i]
- cards[1].each do |card|
.col-md-12.card_line.tooltip-card{style: "padding:0px;margin:0;", data: {id: card.id, color: card.color, type: card.card_class, rarity: card.rarity, set: card.card_set, eng_title: card.eng_title&.downcase || '', title: card.title&.downcase}}
= link_to card, class: "#{color_class(card.color)}-card-tooltip card-tooltip", target: '_blank' do
%div.cards_gr{class: "bg1_#{color_class(card.color)}", style: "border-left: 4px solid #{rarity_color(card.rarity)};"}
%div{:style => "padding-right: 10px;float:left;"}
- if card.card_class == 1
= image_tag card.hero_icon.url(:small), width: '20px', height: '20px', style: "margin-top:-4px;"
- elsif card.card_class == 3
%span{:style => "font-size:14px;font-weight:bold;"}= card.item_gold
- else
%span{:style => "font-size:14px;font-weight:bold;"}= card.manacost
= card.title
我怎么能修好它?
最佳答案
你的问题是卡[4]为零,你必须检查它,可能会设置如下默认值:
items.each_with_index do |cards, i|
if cards[4]
data = { color: cards[4].first.color,type: cards[4].first.card_class }
else
data = { color: 'default', type: 'default' }
end
.col-md.card-column{data: data}
end
关于ruby-on-rails - 如何在 View Ruby中修复“NoMethodError”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53964868/