Question

I think this one is a little hard to explain by the title alone, so here's some I code I came up with:

Rails View Helper

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

View (haml) that uses the Helper

- 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

In summary, the Helper successfully returns a link_to or a content_tag, but I need to keep concatenating or adding more tags inside the tag returned by the Helper (through a block). It seems this should be easy to do in Rails, What am I missing?

Thanks in advance!

Was it helpful?

Solution

Try this for your helper method,

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

This will yield the content in an a or div tag from the block defined in your view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top