views:

128

answers:

1

Hello reader,

with the following routes I try to achive the goal, that I can present static resources like terms of use, imprint and so on in different languages using different urls.

I defined two example routes for my imprint like that:

map.imprint ':lang/impressum', :controller => "statics", :action => "imprint", :requirements => {:lang => /de/}
map.imprint ':lang/imprint', :controller => "statics", :action => "imprint", :requirements => {:lang => /en/}

Now in my view I try to use the path/url helper like that:

<%= link_to(t(statics.imprint.linkname), imprint_url(:lang => session[language])) %>

where there session[:language] is "de" or "en".

Thats results in a working link for the de route. But the english one fails. If I change the order of the routes, it's vice versa, and the english one works, while the german one fails.

The error always reads like that:

imprint_url failed to generate from {:controller=>"statics", :lang=>"de", :action=>"imprint"}, expected: {:controller=>"statics", :action=>"imprint"}, diff: {:lang=>"de"}

Can anyone help out with this?

Thanks. Jason

+2  A: 

As far as I know, you cannot map two routes to the same name like that.

You would need to rename one of them, ie
map.impressum
map.imprint

When Rails looks up the route, it will stop at the first one that it finds, that's why your 'de' links are working.

Ethan Gunderson
... it seems like that. Hoped, that :requirements would discard the "wrong" route so that the next one can be selected. Obviously not. Thanks, that helped me.
Jason Nerer
@Jason I wish that were the case. I actually made the same assumption a couple of weeks ago. Rails is smart, but not that smart ;)
Ethan Gunderson