0

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?

3
  • possible duplicate of Rails 4 custom helper method with form doesn't get output in view - fully Commented Sep 30, 2015 at 9:57
  • I am familiar with the solution in the linked article however it does now work in this scenario. Concat can only be used within the link_to and accordingly does not allow multiple link_to helpers to be chained. Commented Sep 30, 2015 at 10:00
  • fontawesome-sass is giving you icon(:flag). just sayin Commented Sep 30, 2015 at 11:52

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

The example above is a simplified version of a helper with much logic that needs to output two links from the one helper method.
But what you showed is not a helper method, it's just a module. Can you encapsulate this logic into a method, then call it from the view?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.