Note that there are some explanatory texts on larger screens.

plurals
  1. POTranslating routes in Rails
    text
    copied!<p>I am trying to do a simple language translation for my routes but I really can't understand why my approach isn't working. I would like to get <code>www.example.com/it/camere</code> by calling <code>rooms_path(locale: :it)</code> and <code>www.example.com/en/rooms</code> by calling <code>rooms_path(locale: :en)</code>.<br> For example, look at the following routes: </p> <pre><code>get ":locale/rooms" =&gt; "home#rooms", constraints: { locale: "en" }, as: "rooms" get ":locale/camere" =&gt; "home#rooms", as: "rooms" </code></pre> <p>I get the following results:</p> <pre><code>app.rooms_path # =&gt; error OK app.rooms_path(locale: "it") # =&gt; 'it/camere' OK app.rooms_path(locale: "en") # =&gt; 'en/camere' Why?!? </code></pre> <p>And with the following routes (notice the second constraint):</p> <pre><code>get ":locale/rooms" =&gt; "home#rooms", constraints: { locale: "en" }, as: "rooms" get ":locale/camere" =&gt; "home#rooms", constraints: { locale: "it" }, as: "rooms" </code></pre> <p>I get the following results:</p> <pre><code>app.rooms_path # =&gt; error OK app.rooms_path(locale: "it") # =&gt; 'it/camere' OK app.rooms_path(locale: "en") # =&gt; error Lol?!? This is weird! </code></pre> <p>Can someone please explain to me why this is happening?<br> I have already read about 3 times the Rails article about routing but It didn't help.</p> <p>Thanks!</p> <p>P.S. I would prefer <strong>not to use plugins</strong> in order to learn :)</p> <p><strong>EDIT:</strong></p> <p>This is my what I get by calling rake routes:</p> <pre><code>rooms GET /:locale/rooms(.:format) home#rooms {:locale=&gt;"en"} rooms GET /:locale/camere(.:format) home#rooms {:locale=&gt;"it"} root /(:locale)(.:format) home#index </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload