开发者

Rails routing with requirements

开发者 https://www.devze.com 2022-12-16 00:35 出处:网络
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.

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开发者_如何学JAVA 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消