ruby - 超薄模板 : How to rescue an exception?

标签 ruby erb slim-lang

我想将以下 erb 代码转换成 slim。

<% begin %>
  <%= some_function %>
<% rescue Exception %>
  <%= some_other_function %>
<% end%>

我的做法是:

- begin 
  = some_function
- rescue Exception 
  = some_other_function

但是这样会报错:

index.slim:34: syntax error, unexpected keyword_ensure, expecting $end

如何使用 slim 正确地挽救异常?

最佳答案

你需要做一个助手。

您应该在该帮助程序中放置开始/救援逻辑。

# my_helper.rb
class MyHelper
  def my_func
    begin
      some_function
    rescue
      some_other_func
    end
  end
end

# slim view
= my_func

关于ruby - 超薄模板 : How to rescue an exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14731301/

相关文章:

ruby-on-rails - Ruby/Slim 是否支持 Markdown ?

ruby-on-rails - 无法在 Rails 助手中捕获 block 的输出

ruby-on-rails - 如何为 rails 和 sqlite 设置时区?

ruby - 如何重用选项散列

ruby - 具有身份验证的私有(private) Ruby Gem 服务器

javascript - Rails 5 在 .erb 文件中崩溃,但仅在服务器上运行时发生

ruby - 如何使用 google-api-ads-ruby gem 添加排除关键字和网络搜索参数?

ruby-on-rails - 在局部文件中本地定义的变量对于调用的 erb 模板是否也可见?

ruby-on-rails - 这个 ruby​​ 语句有什么问题?

ruby-on-rails - 使用 slim 的 form_for 方法