Hi!
The problem:
You may have read the I18n-Guide (
Rails Internationalization (I18n) API — Ruby on Rails Guides).
It says, that the usual way to provide the locale is in the URL, like
example.com/en/books/2. But:
map.resources :books, :path_prefix => ‘/:locale’
[…]
This solution has currently one rather big downside. Due to the
default_url_options implementation, you have to pass the :id option
explicitely, like this: link_to ‘Show’, book_url(:id => book) and not
depend on Rails’ magic in code like link_to ‘Show’, book.
Actually this is not a problem of default_url_options, but of
polymorphic_url, which is utilized by link_to in the example. It
generates
the book_url and passes an array into the generated route. This array
has
only one item, the book id. book_url tries to fill in the id into the
first
parameter of the url, which is the locale (/:locale/books/:id), which
leaves
the :id unsatisfied, raising an exception.
The solution:
With my patch, polymorphic_url will pass an hash instead of an array to
the
generated route. It may look like {:id => 2}. It works also with nested
resources: e.g.: {:author_id => 3, :id => 2}
The patch:
I’ve created a patch for Rails 3 and Rails 2-3-stable and put them in a
ticked on lighthouse:
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3394-polymorphic_url-fails-to-generate-routes-with-path_prefix-with-a-variable
I hope, you’ll find this patch useful and it gets accepted into rails
soon.
Regards from Hamburg, Germany
Johannes