在选择的条件中包含空白选项
问题描述:
我必须显示可以评分的国家列表。如果一个国家已经被评为那么用户不能“unrate”它在选择的条件中包含空白选项
# countries index
= render @countries
# countries/_country
= form_for country do |f|
= f.select :rating,
Rating.options,
include_blank: "False if country is rated. True otherwise"
我试图通过一个进程来include_blank选项,但它没有工作
我可以使用助手来计算它看起来像选项这个:
def rating_options(country)
if country.rating.present?
Rating.options
else
[""] + Rating.options
end
end
有没有更好的方法来在条件选择标签中包含空白选项?
答
你可以传递到include_blank
选项的任何辅助方法或条件
= form_for country do |f|
= f.select :rating,
Rating.options,
include_blank: f.object.rating.empty?
谢谢你,该解决方案是如此简单 - 我可以计算include_blank在线!我甚至可以使用国家局部变量 - 'include_blank:country.rating.blank?'出于某种原因,我认为我需要lambda或helper – Hirurg103