You are here: Forums Ask a Rails expert Catch invalid locale values in...
Posted in Forums : Ask a Rails expert
Authority 0
Posting Rating 69
Sign in to rate this post
|
Hi, Could someone tell me how I could catch invalid locale values in the url? Thanks |
|
Authority 62
Posting Rating 0
Sign in to rate this post
|
You should use a before filter in your Application Controller that set a default local value is it are invalid. |
Authority 0
Posting Rating 69
Sign in to rate this post
|
I’ve added a the before_filter (http://pastie.org/508180) But when I surf to domain.tld/nl ->but there is a route error (see pastie). I’d like to surf to a url, just with a /language, than it should be forwarded to the sites controller. domain.tld/nl -> sites controller How could I do this? |
Authority 12
Posting Rating 91
Sign in to rate this post
|
Try to see this documentation and find option ’:requirements’ where you can specify the regular expression for your param. For example :
|
Authority 50
Posting Rating 99
Sign in to rate this post
|
You didn’t include your route definitions in your pastie, but in your first post you wrote that you set up your routes to match this pattern: /:locale/:controller The examples you gave don’t match this pattern, because they have a :locale component, but not a :controller component. So, without a matching route you get a routing error and your before_filter never comes into play. The locale component of your URL is simply a prefix, so you still need the rest of the URL. Try something like /fr/sites You’ll also probably need to supply an action, although this example should default to #index. Using the requirements option in the route definition will restrict your valid routes to those that match the specified regular expressions, but if they don’t match you’ll end up with routing errors again. As for your before_filter, you probably want to only redirect if the locale is not valid. Once you’re in a controller’s before_filter you know the URL has matched a valid route; if the locale is appropriate then you allow the controller action to proceed normally. This way you can reuse the before_filter on other controllers that have the same locale requirements. |
Authority 0
Posting Rating 69
Sign in to rate this post
|
Thanks Adam, I’ve modified my route description and Robins. So I ended up with : This works without any problem, I just have to adjust some settings involving the sessions and cookies. Thanks everybody for helping |
Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController
Ask a Rails expert : Best way to structure a database for a large/static dataset
Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)
Ask a Rails expert : Above Ground Pool Supplies
Ask a Rails expert : How to get url params in observer or model in Rails 3.1
Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?
Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?
Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )
Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob
Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )