Rubén T.F.
Rubén T.F.

Reputation: 134

Rails 3 localized routes and show resources

I'm rewriting my routes on a Rails 3.2 server, so the language is a part of the URL. My routes.rb file is like that:

scope ":lang", :lang => /es|fr|it|pt|en|de/ do
    resources :users
end

Pretty much all URLs work perfectly by appending the language strings, i.e:

http://mydomain/es/somecontroller/someaction

The problem comes with the show actions, the links doesn't work. This line:

<%= link_to "Show", @my_user %>

Causes a "Routing Error" like this:

No route matches {:action=>"show", :controller=>"users", :lang=>##User serialization##}

What am I doing wrong?

EDIT: Ok, solution found. Thanks to Cage and a little research there's the solution.

Only need a method on ApplicationController like this:

def self.default_url_options
  { :lang => FastGettext.locale }
end

And everything works flawlessly!! No need to rewrite links at all.

Upvotes: 3

Views: 565

Answers (1)

abhas
abhas

Reputation: 5213

For show, edit, destroy you should be passing :id of the object in path else it will show an error. For e.g.

 <%= link_to "Show", user_path(@my_user.id) %>

Go through this link it will help you a lot in case of routing http://guides.rubyonrails.org/routing.html

Upvotes: 1

Related Questions