Condition in href doesn't work

Hello, I have this code:

- locales.each do | l |
  - if l == I18n.locale
  - else 
    = link_to (l == "en" ? "/" : "/#{l}/") do
      %span #{l.downcase}

where basically I want for all languages other than English links to have /xx/ in URL but English will point directly to root (/). For some reason, this condition does not work and I ending up having /en/ instead of /.

I just quickly checked in my project and your code works correctly. But just to be strict: locales returns an array of symbols (locale is a symbol), so you could use symbol comparison instead of just string "en". Fortunately automatic type-cast is working for you. BTW, why not get rid of empty if? And no need to use Ruby-interpolation in %span, just use =

- locales.each do | l |
  - unless l == I18n.locale
    = link_to (l == :en ? "/" : "/#{l}/") do
      %span= l.downcase

Hey!

Thanks for the reply. Okay, so I added your code but something is not right with the URL that this "/" generates. So assuming I am now on http://127.0.0.1/fr/ (French version of the website, generated HTML looks like this:

<a href="./">
  <span>en</span> 
</a>

Noticed that ./? Because of that, the link won’t go to root of the website, instead it will stay at whatever language it’s currently on - so in this case - French. Something is wrong with the way my Middleman generates HTML?

Solution for now is to add the . at the beginning so it’s:

= link_to (l == :en ? "/." : "/#{l}/") do

Although I don’t know whether it’s bulletproof (something I need to test in various browsers and mobile devices).

Let’s not spam and solve all of this in your other post, OK?

1 Like