在 Helper 中使用 Rails ERB 标签

Use Rails ERB tag in Helper

使用 Rails 4. 我有以下内容:

module ShopsHelper
  def edit_shop(shop)
    link_to edit_shop_path(shop), target: "_blank" do
      raw("<i class='fa fa-edit'></i> Update")
    end
  end
end

请注意,我使用的 <i class='fa fa-edit'></i> 用于 Font Awesome,但 Font Awesome gem 提供的 <%= icon "edit" %> 更干净。如何在助手中使用 icon 标签?

如果您调用助手,如文档中所述:

icon "edit"
# => <i class="fa fa-edit"></i>

rails 助手与任何其他方法一样工作,返回的值在 ERB 模板中被替换。

当您使用其中一个表单助手作为块的包装器时 (... do %>) rails 在块之后关闭标签。

link_to edit_shop_path(shop), target: "_blank" do
  raw("<i class='fa fa-edit'></i> Update")
end

变成:

<a .... >
  <i></i>
</a>

您应该可以替换:

<i class='fa fa-edit'></i> Update

与:

icon "edit"

module ShopsHelper
  def edit_shop(shop)
    link_to edit_shop_path(shop), target: "_blank" do
      raw icon('edit', 'Update')
    end
  end
end