Monday, June 20, 2016

HTML code inside a link in Rails

This is quite simple, indeed, but I see many people asking about this topic in StackOverflow and then this post may be of some help.

Let's see how we may create a link wrapping some HTML code, like this:

We tend to forget that the <%= link_to %> may start a block if you append a 'do' at the end of it. then we may do

It is even possible to mix image and other HTML elements in your link, doing something like this:

As you may see, Rails offers many possibilities if you need complex links. But remember that HTML specification won't allow some elements inside a <a></a>, although most browsers will render almost anything you imagine. And some things you can't imagine too!

To see a good example of this feature, visit this StackOverflow question and see my answer there.

See you next post!