簡體   English   中英

如何將塊傳遞給助手返回的“ link_to”?

[英]How can I pass a block to a 'link_to' returned from a Helper?

我認為僅憑標題很難解釋這個問題,因此我想出了一些我編寫的代碼:

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

使用助手的視圖(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中應該很容易做到,我想念的是什么?

提前致謝!

試試這個作為您的輔助方法,

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

這將從視圖中定義的塊中的adiv標簽中產生內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM