Have you experienced problems with link_to
helper used with the block argument? According to Padrino doc we can use this:
link_to(‘/dashboard’, :class => ‘blocky’) { …content… }
I tried to make an image link: <a …><img …></a>
and this construct with block works when used in some of my helpers, but it doesn’t work as expected in the ERB template.
The code that succesfuly works (in the helper):
return link_to(i18n_url_for(new_locale), :title => title_attr) { content_tag(:div, "", :id => flag_id, :class => flag_class) }
i18n_url_for
is my another helper method, generating string (URL), anyway the DIV tag is succesfully generated with this code (as part of the helper building the language switching flags in the header), like this:
<a title="Русский" href="ru/index.html"><div id="flag_ru" class="header_flag"></div></a>
<a title="Polski" href="pl/index.html"><div id="flag_pl" class="header_flag"></div></a>
(Don’t worry about empty DIV, it’s CSS-styled.)
The problem is in a different situation, when I try to use similar construct for the image link:
<div id="portfolio_tiles">
<% data.slides.homepage.each do |slide| %>
<div class="tile"><figure>
<%= link_to("portfolio/" << slide.url, title: slide.title ) { image_tag("websites/555/" << slide.filename, title: slide.title, width: 278, height: 221) } %>
<figcaption><%= slide.title %></figcaption>
</figure></div>
<% end %>
</div>
This loop is feeded with local data but the image_tag
generates empty string (without any error message). So the generated HTML is:
<a title="My Slide Title" href="portfolio/spatlegal.html"></a>
and the link body is empty. Trying to debug this, I checked, that substituting image_tag
with a plain string – doesn’t help either:
<%= link_to("portfolio/" << slide.url, title: slide.title ) { "DUMMY" } %>
This still generates empty link. I also checked, that the image_tag
used without the block-context works OK:
<%= image_tag("websites/555/" << slide.filename, title: slide.title, width: 278, height: 221) %>
It generates IMG
tags correctly, but without the A
tag, obviously.
I had to use plain HTML code with url_for
helper to make it work:
<a href="<%= url_for ("portfolio/" << slide.url) %>", title="<%= slide.title %>"><%= image_tag("websites/555/" << slide.filename, title: slide.title, width: 278, height: 221) %></a>
This code works OK. Any ideas what’s wrong with the non-working block variant? Thanks.