[英]Correct format for writing conditionals in ruby and then converting output into string using string interpolation inside HAML tag
我正在修复项目前端的 ruby on rails 开源项目中的一个错误。 我是 ruby on rails、HAML 等的新手。以下代码行给我带来了很多麻烦。
我想知道格式化这个的正确方法是什么。 此外,有没有办法编写一个辅助函数来将条件转换为函数调用? 任何帮助将不胜感激。
我尝试了几种格式,但开发人员希望我将 if-else 分成几行。 我无法完成这项工作。
6: %strong =
7: "#{
8: - if @enterprise.is_primary_producer
9: = t('.producer_profile')
10: - else
11: = t('.profile')
我希望渲染视图,但我收到了语法错误。
像这样的东西?
%strong
- if @enterprise.is_primary_producer
= t('.producer_profile')
- else
= t('.profile')
就个人而言,我会做这样的事情:
- t_key = @enterprise.is_primary_producer ? '.producer_profile' : '.profile'
%strong= t(t_key)
如果你想把它移动到一个助手,只需在 application_helper.rb 中定义它
def some_name_for_the_method(enterprise)
t_key = enterprise.is_primary_producer ? '.producer_profile' : '.profile'
I18n.t(t_key)
end
并在视图上
%strong= some_name_for_the_method(@enterprise)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.