Dercni
Dercni

Reputation: 1224

Rails generate HTML in custom helper

I have the following simplified helper which works:

module MyHelper
  def widget
    link_to new_flag_path do
      content_tag(:i, '', class: "fa fa-flag")
    end
  end
end

I would like to output a second link such as:

  module MyHelper
    def widget
      link_to new_flag_path do
        content_tag(:i, '', class: "fa fa-flag")
      end
      link_to new_comment_path do
        content_tag(:i, '', class: "fa fa-comment")
      end
    end
  end

The solution outlined in the pugautomatic article uses "concat" to concatenate multiple helpers within a single block helper. http://thepugautomatic.com/2013/06/helpers/ This works for standard link_to helpers such as:

module MyHelper
  def widget
    concat link_to("Hello", hello_path)
    concat " "
    concat link_to("Bye", goodbye_path)
  end
end

When using a glyphon in a href you need to use a link_to block helper such as:

link_to new_comment_path do
  content_tag(:i, '', class: "fa fa-comment")
end

Concat does not allow me to concat multiple link_to block helpers such as:

module MyHelper
  def widget
    concat link_to new_flag_path do
      content_tag(:i, '', class: "fa fa-flag")
    end
    concat link_to new_comment_path do
      content_tag(:i, '', class: "fa fa-comment")
    end
  end
end

What is the solution in this scenario?

Upvotes: 0

Views: 1516

Answers (1)

Arsen
Arsen

Reputation: 10951

I think you have to put each link_to to separate method:

module MyHelper
  def link_to_flag_page
    link_to new_flag_path do
      content_tag(:i, '', class: "fa fa-flag")
    end
  end

  def link_to_new_comment
    link_to new_comment_path do
      content_tag(:i, '', class: "fa fa-comment")
    end
  end
end

And call them one by one

Upvotes: 2

Related Questions