Rails link_to do and link_to_if Helpers
If you are building web applications with Ruby on Rails then you likely have used the link_to
helper to build hyperlinks. It’s a more developer-friendly way of adding links to ERB templates and can be used with other helpers like new_session_path
provided by devise. So…
link_to do
But sometimes we want to make links out of images. Well, link_to
accepts a block to make this work. We can pass it an image tag via another helper.
link_to_if and link_to_unless
Some links only need to appear under certain conditions. For this, we have link_to_if
and link_to_unless
to the rescue. It accepts a condition and is cleaner for single links than creating an entire if
block in your view.
These are just some nice ways to clean up your code and show off a few nice tricks that Ruby on Rails provides.