Ruby 将扩展的 if 语句转换为一行
Ruby convert expanded if statement into one-liner
鉴于这两个部分之间只有微小的差异,是否有更好的方式来编写此 if/else 语句?
if current_admin_user.role?(:admin)
f.input :invoice_file_date, as: :datepicker if f.object.invoice_file.present?
else
f.input :invoice_file_date, as: :datepicker, :input_html => { :disabled => true } if f.object.invoice_file.present?
end
我不记得 :disabled => false
是否会执行您想要的操作,或者那是否是 "only check if the attribute exists not the value" 中的一个,但这会起作用。不过不确定它是否更好。
f.input :invoice_file_date, as: :datepicker,
:input_html => { :disabled => !current_admin_user.role?(:admin) } if f.object.invoice_file.present?
鉴于这两个部分之间只有微小的差异,是否有更好的方式来编写此 if/else 语句?
if current_admin_user.role?(:admin)
f.input :invoice_file_date, as: :datepicker if f.object.invoice_file.present?
else
f.input :invoice_file_date, as: :datepicker, :input_html => { :disabled => true } if f.object.invoice_file.present?
end
我不记得 :disabled => false
是否会执行您想要的操作,或者那是否是 "only check if the attribute exists not the value" 中的一个,但这会起作用。不过不确定它是否更好。
f.input :invoice_file_date, as: :datepicker,
:input_html => { :disabled => !current_admin_user.role?(:admin) } if f.object.invoice_file.present?