如何在重定向到 Rails 中的 link 之前使用 'if' 语句
How to use an 'if' statement before redirecting to a link in Rails
我在一个名为 analysis_result.rb
的模型中定义了这个:
def total_matches
return 0 unless self.patterns
self.patterns.sum do |_, v|
matches = v.matches.try(:count) || 0
if v.additional.present? && v.additional['ggroup'].present?
bc_matches = v.additional['ggroup'].try(:count) || 0
else
bc_matches = 0
end
matches + bc_matches
end
end
我试图在名为 _rable_row.haml
的视图中使用它,以便事先检查 total_matches
是否为 0。如果它是 0,我想显示部分而不是让用户转到 link。
这是要检查的视图中的代码 if analysis.results.total_matches != 0
:
%tr.form-table__row{ class: ('form-table__row--disabled' if analysis.processing?) }
%td.form-table__data= check_box_tag "checkbox_object_ids[]", analysis.id
%td.form-table__data
- if analysis.results.total_matches == 0
= render partial: 'partials/shared/empty'
- elsif analysis.results.total_matches != 0
= link_to analysis.title, analysis, class: 'js-toggle', data: { href: "loading-#{analysis.id}" }
- unless analysis.viewed
%span.dashboard__icon.dashboard__icon--small.fa.fa-circle.text-info{ aria: { hidden: 'true' }, title: 'New' }
我得到undefined method 'total_matches' for #<Mongoid::Criteria:0x00007fc51c5e3720>
你的问题出在方法本身的定义上。您已在 analysis_result.rb
上声明了您的方法 total_matches
,但您正在调用 analysis.results.total_matches
。
我会写 analysis.total_matches
。
奖金:
我建议在你的方法之上添加一个保护子句total_matches
def total_matches
return 0 unless self.patterns
# ...
end
根据我在您更新的问题中看到的情况,您的 AnalysisResult 属于 Analysis。 total_matches是Analysis的实例方法。
但是你在这里像 analysis.results.total_matches 一样调用它,analysis.results 会给你一个活动记录数组作为分析 has_many 结果,你试图在上面调用 total_matches。
您应该尝试 analysis.results。找到一个实例并在其上调用 total_matches。
例如:analysis.results.last.total_matches(我只是以.last为例)
我用不同的方式解决了这个问题:
- if analysis.results.sum(&:total_matches) != 0
只改成这个
我在一个名为 analysis_result.rb
的模型中定义了这个:
def total_matches
return 0 unless self.patterns
self.patterns.sum do |_, v|
matches = v.matches.try(:count) || 0
if v.additional.present? && v.additional['ggroup'].present?
bc_matches = v.additional['ggroup'].try(:count) || 0
else
bc_matches = 0
end
matches + bc_matches
end
end
我试图在名为 _rable_row.haml
的视图中使用它,以便事先检查 total_matches
是否为 0。如果它是 0,我想显示部分而不是让用户转到 link。
这是要检查的视图中的代码 if analysis.results.total_matches != 0
:
%tr.form-table__row{ class: ('form-table__row--disabled' if analysis.processing?) }
%td.form-table__data= check_box_tag "checkbox_object_ids[]", analysis.id
%td.form-table__data
- if analysis.results.total_matches == 0
= render partial: 'partials/shared/empty'
- elsif analysis.results.total_matches != 0
= link_to analysis.title, analysis, class: 'js-toggle', data: { href: "loading-#{analysis.id}" }
- unless analysis.viewed
%span.dashboard__icon.dashboard__icon--small.fa.fa-circle.text-info{ aria: { hidden: 'true' }, title: 'New' }
我得到undefined method 'total_matches' for #<Mongoid::Criteria:0x00007fc51c5e3720>
你的问题出在方法本身的定义上。您已在 analysis_result.rb
上声明了您的方法 total_matches
,但您正在调用 analysis.results.total_matches
。
我会写 analysis.total_matches
。
奖金:
我建议在你的方法之上添加一个保护子句total_matches
def total_matches
return 0 unless self.patterns
# ...
end
根据我在您更新的问题中看到的情况,您的 AnalysisResult 属于 Analysis。 total_matches是Analysis的实例方法。
但是你在这里像 analysis.results.total_matches 一样调用它,analysis.results 会给你一个活动记录数组作为分析 has_many 结果,你试图在上面调用 total_matches。
您应该尝试 analysis.results。找到一个实例并在其上调用 total_matches。
例如:analysis.results.last.total_matches(我只是以.last为例)
我用不同的方式解决了这个问题:
- if analysis.results.sum(&:total_matches) != 0
只改成这个