Reputation: 4677
I am trying to convert the following html into a ruby helper:
<a href="home.html" class="hvr-sweep-to-bottom">
<i class="flaticon-insignia"></i>
<span>home</span>
</a>
Here is my code so far that does not work:
link_to home_path(media), class: 'hvr-sweep-to-bottom' do
content_tag(:i, class: 'flaticon-insignia') {}
content_tag(:span) do
menu_item.title
end
end
This produces the following output:
<a class="hvr-sweep-to-bottom" href="/">
<span>Home</span>
</a>
How do I get this to work?
Upvotes: 1
Views: 128
Reputation: 28285
link_to
, when passed a block, will wrap <a>
tags around whatever the block returns. In your code:
link_to home_path(media), class: 'hvr-sweep-to-bottom' do
content_tag(:i, class: 'flaticon-insignia') {}
content_tag(:span) do
menu_item.title
end
end
the only thing being returned is the final method call (content_tag(:span) do ... end
). There are multiple ways of writing this, such as using String#concat
, but here's how I normally do it:
link_to home_path(media), class: 'hvr-sweep-to-bottom' do
link_text = content_tag(:i, class: 'flaticon-insignia') {}
link_text << content_tag(:span) do
menu_item.title
end
end
Note also that in your example, menu_item.title
is Home
not home
; and home_path(media)
is /
not home.html
- so you still won't end up with identical HTML. However, those are both separate issues for you to think about.
Upvotes: 1
Reputation: 34603
Concatenate the content_tags together, using the + symbol
:
link_to home_path(media), class: 'hvr-sweep-to-bottom' do
content_tag(:i, '', class: 'flaticon-insignia') + content_tag(:span, menu_item.title)
end
That should do it
Upvotes: 0