HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsMinor

Best way to include image_tag inside link_to

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bestincludewayimage_taglink_toinside

Problem

What is the most elegant and readable way to in include (potentially long) image_tag calls inside of link_to?

Example

Solution

You could create a helper to keep your views less bloated. For example:

app/helpers/application_helper.rb

...
def image_link_to(image_path, url, image_tag_options = { }, link_to_options = { })
link_to url, link_to_options do
image_tag image_path, image_tag_options
end
end
...


app/views/pages/show.html.haml

...
= image_link_to "buckminsterfullerene.png", my_url_helper, { size: '210x60', alt: 'Molecular structure of Buckminsterfullerene' }, { title: 'Buckminsterfullerene', rel: 'tooltip', relative: true }
...

Context

StackExchange Code Review Q#20812, answer score: 2

Revisions (0)

No revisions yet.