- if clean_book_intro_component(:literary_works).present?
  %h2 Other Books Related to #{@book.title}
  %p.fact-text
    = clean_book_intro_component(:literary_works)

上面的代码可以只调用clean_book_intro_component(:literary_works)一次吗?
clean_book_intro_component的实现
def clean_book_intro_component(component)
  sanitize @book.intro.send(component), tags: %w(span b i p a), attributes: %w(id class style href)
end

最佳答案

- clean_book_intro_component(:literary_works).tap do |cbic|
  - if cbic.present?
    %h2 Other Books Related to #{@book.title}
    %p.fact-text
      = cbic

也许我搞错了haml,但我的想法是使用tap而不是显式地保存调用的结果

关于ruby-on-rails - 重构以下代码行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34607790/

10-13 01:26