Question

In my project I am using HAML. I use a construct similar to the following fragment throughout my project:

%a.social.twitter{:href => '...'}
  %span.text Twitter
  %span.twitter-icon

I want to write a helper to generate this fragment for me:

def social_network(name)
 link_to(...) do
   # generate span.text and span.twitter.icon
 end
end

Ideally I don't want to just pass in some HTML disguised in string form to the link_to block. I prefer to use a markup builder or an API.

What built-in options does Rails have that can be used for this purpose?

Was it helpful?

Solution

Rails has content_tag and tag in ActionView::Helpers::TagHelper which

Provides methods to generate HTML tags programmatically when you can’t use a Builder.

For example:

def social_network(name, klass)
  link_to(...) do
    content_tag(:span, name)
    tag(:span, class: klass)
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top