0

我认为这个单凭标题有点难以解释,所以这是我想出的一些代码:

Rails 视图助手

module SplashHelper

  def send_link_or_tag(link=true)
    if link
      link_to nil, root_path, class: 'to-block'
    else
      content_tag :div, 'The content'
    end
  end
end

使用 Helper 的视图(haml)

- 5.times do |i|
  - if i%2 == 0

    = send_link_or_tag do
      -#THE PROBLEM IS THAT I CAN'T ADD CONTENT TO THE
        RETURNED link_to (<a> tag) in this case the <p> tag
        INSIDE THIS BLOCK!
      %p = 2 + 2 

  - else

    = send_link_or_tag false do
      -# SAME PROBLEM HERE.
      %p = 3 * 3

总之,Helper 成功返回了link_tocontent_tag ,但我需要在 Helper 返回的标签内继续连接添加更多标签(通过块)。看来这在 Rails 中应该很容易做到,我错过了什么?

提前致谢!

4

1 回答 1

1

试试这个作为你的辅助方法,

def send_link_or_tag(link=true)
  if link
    link_to root_path, class: 'to-block' do
      yield
    end
  else
    content_tag :div do
      yield
    end
  end
end

这将在视图中定义的块中生成aordiv标记中的内容。

于 2014-04-06T23:48:15.570 回答