She*_* Wu 4 ruby haml ruby-on-rails
我在HAML文档中有一些内插的ruby代码,如下所示:
:javascript
a = { 'something' : "#{model.attribute.present? ? method(parameter) : ''}" }
Run Code Online (Sandbox Code Playgroud)
但是,方法(参数)抛出一些带有参数的异常,所以我想解救异常.抛出异常时,我希望将'something'键映射到其他东西.
我不清楚如何使用HAML代码/ ruby插值执行此操作的确切语法.我尝试了几件事,但它没有用.
谢谢!
你应该走这条路
model.attribute.present? ? method(parameter) : ''
Run Code Online (Sandbox Code Playgroud)
并从中制作一个帮助方法
class ApplicationHelper
def some_method
model.attribute.present? ? method(parameter) : ''
rescue
"something else" # this is your value in case of exception
end
end
Run Code Online (Sandbox Code Playgroud)
然后你的HAML变得微不足道
:javascript
a = { 'something' : "#{some_method}" }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1215 次 |
| 最近记录: |