Locale parameter on URL

Hello guys. I just started learning Rails and one of the first things
I was interested was the localization of my website.
I started looking the rails-i18n and it worked beautifully, except for
some things. e.g.: i wanted the url to be /en/books instead of
the locale=en, so I tried what they say on

about the :path_prefix on routes.rb. But it just didn’t worked…
Here is my routes.rb:

ActionController::Routing::Routes.draw do |map|
map.resources :mannequins, :path_prefix => ‘/:locale’
map.resources :recipes, :path_prefix => ‘/:locale’

The priority is based upon order of creation: first created →

highest priority.

Sample of regular route:

map.connect ‘products/:id’, :controller => ‘catalog’, :action =>

‘view’

Keep in mind you can assign values other than :controller

and :action

Sample of named route:

map.purchase ‘products/:id/purchase’, :controller =>

‘catalog’, :action => ‘purchase’

This route can be invoked with purchase_url(:id => product.id)

Sample resource route (maps HTTP verbs to controller actions

automatically):

map.resources :products

Sample resource route with options:

map.resources :products, :member => { :short => :get, :toggle

=> :post }, :collection => { :sold => :get }

Sample resource route with sub-resources:

map.resources :products, :has_many =>

[ :comments, :sales ], :has_one => :seller

Sample resource route with more complex sub-resources

map.resources :products do |products|

products.resources :comments

products.resources :sales, :collection => { :recent => :get }

end

Sample resource route within a namespace:

map.namespace :admin do |admin|

# Directs /admin/products/* to Admin::ProductsController (app/

controllers/admin/products_controller.rb)

admin.resources :products

end

You can have the root of your site routed with map.root – just

remember to delete public/index.html.
map.root :controller => “recipes”, :path_prefix => ‘/:locale’

See how all your routes lay out with “rake routes”

Install the default routes as the lowest priority.

Note: These default routes make all actions in every controller

accessible via GET requests. You should

consider removing or commenting them out if you’re using named

routes and resources.
map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
end

but when I try to access http://localhost:3000/fr/recipes I get

ActionController::RoutingError in Recipes#index
Showing app/views/recipes/index.html.erb where line #15 raised:
recipe_url failed to generate from {:locale=>#<Recipe id: 2, ti…

Can anyone shed some light on this for me?

‘-’

Can you show me line #15 of app/views/recipes/index.html.erb?

Line #15 contains the following:

<%= link_to 'Show', recipe %>

That’s it…

Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

2010/4/8 Andrés Mejía [email protected]

Can you try this:

<%= link_to “Show” recipe_path(@recipe) %>

In the conntroller show you must pass a variable named @recipe with the
object

2010/4/9 Paulo H. [email protected]

+55 38 9141 5400

ActionController::Routing::Routes.draw do |map|
and :action

Sample resource route with options:

products.resources :sales, :collection => { :recent => :get }

remember to delete public/index.html.
map.connect ‘:controller/:action/:id.:format’
‘-’


Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

Do yo have this?

app/controllers/application_controller.rb

def default_url_options(options={})
logger.debug “default_url_options is passed options:
#{options.inspect}\n”
{ :locale => I18n.locale }
end

Ah! in the console put this:
rake routes

To see what is generating

2010/4/9 Paulo H. [email protected]

=> :delete %>
GTALK: [email protected]

Paulo H. Vieira Neves

Here is my routes.rb:

map.connect ‘products/:id’, :controller => ‘catalog’, :action =>

automatically):

Sample resource route with more complex sub-resources

end

consider removing or commenting them out if you’re using named

.
.
.
"Caminar sobre el agua y desarrollar software a partir de unas
"Caminar sobre el agua y desarrollar software a partir de unas
For more options, visit this group at
For more options, visit this group at
http://groups.google.com/group/rails-i18n?hl=en.


Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

Sorry are you iterating in a variable named @recipes?

In this case, the link must be this:

<% @recipes.each do |recipe| %>
<%= link_to “Show” recipe_path(recipe) %>
<% end %>
El 9 de abril de 2010 13:29, Andrés gutiérrez
[email protected]escribió:

GTALK: [email protected]

I was interested was the localization of my website.
map.resources :recipes, :path_prefix => ‘/:locale’

Sample of named route:

=> :post }, :collection => { :sold => :get }




Experiencia es lo que obtienes, cuando no obtienes lo que quieres.


“Caminar sobre el agua y desarrollar software a partir de unas
especificaciones es fácil. si ambas están congeladas.”
Edward V. Berard, ingeniero informático.


Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

Yeah, I am iterating… Here’s the code:

<% @recipes.each do |recipe| %>

<%=h recipe.title %> <%=h recipe.author %> <%=h recipe.description %> <%= link_to 'Show', recipe_path(recipe) %> <%= link_to 'Edit', edit_recipe_path(recipe) %> <%= link_to 'Destroy', recipe, :confirm => 'Are you sure?', :method => :delete %> <% end %>

But I got the same error…

Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

2010/4/9 Andrés gutiérrez [email protected]

I have that on application_controller.rb, and when I ran ‘rake routes’,
I
got this:

mannequins GET    /:locale/mannequins(.:format)

{:controller=>“mannequins”, :action=>“index”}
POST /:locale/mannequins(.:format)
{:controller=>“mannequins”, :action=>“create”}
new_mannequin GET /:locale/mannequins/new(.:format)
{:controller=>“mannequins”, :action=>“new”}
edit_mannequin GET /:locale/mannequins/:id/edit(.:format)
{:controller=>“mannequins”, :action=>“edit”}
mannequin GET /:locale/mannequins/:id(.:format)
{:controller=>“mannequins”, :action=>“show”}
PUT /:locale/mannequins/:id(.:format)
{:controller=>“mannequins”, :action=>“update”}
DELETE /:locale/mannequins/:id(.:format)
{:controller=>“mannequins”, :action=>“destroy”}
recipes GET /:locale/recipes(.:format)
{:controller=>“recipes”, :action=>“index”}
POST /:locale/recipes(.:format)
{:controller=>“recipes”, :action=>“create”}
new_recipe GET /:locale/recipes/new(.:format)
{:controller=>“recipes”, :action=>“new”}
edit_recipe GET /:locale/recipes/:id/edit(.:format)
{:controller=>“recipes”, :action=>“edit”}
recipe GET /:locale/recipes/:id(.:format)
{:controller=>“recipes”, :action=>“show”}
PUT /:locale/recipes/:id(.:format)
{:controller=>“recipes”, :action=>“update”}
DELETE /:locale/recipes/:id(.:format)
{:controller=>“recipes”, :action=>“destroy”}
root /:locale
{:controller=>“recipes”, :action=>“index”}
/:controller/:action/:id
/:controller/:action/:id(.:format)

Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

2010/4/9 Andrés gutiérrez [email protected]

I think so, but doesn’t change the fact that I am getting the error…

Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

2010/4/9 Andrés gutiérrez [email protected]

That is correct no?

2010/4/9 Paulo H. [email protected]

{:controller=>“mannequins”, :action=>“edit”}
new_recipe GET /:locale/recipes/new(.:format)
{:controller=>“recipes”, :action=>“index”}

end

<% end %>

<% end %>

+55 38 9141 5400

[email protected]> wrote:

Here is my routes.rb:

map.connect ‘products/:id’, :controller => ‘catalog’, :action

Sample resource route (maps HTTP verbs to controller actions

controllers/admin/products_controller.rb)

Note: These default routes make all actions in every controller

Showing app/views/recipes/index.html.erb where line #15 raised:
To unsubscribe from this group, send email to
To unsubscribe from this group, send email to
To unsubscribe from this group, send email to

[email protected][email protected]
[email protected][email protected]


.
.
For more options, visit this group at
http://groups.google.com/group/rails-i18n?hl=en.


Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

Enclosed image:[image: rubyerror.png]
Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

2010/4/9 Andrés gutiérrez [email protected]

Can you put the complete error please

2010/4/9 Paulo H. [email protected]

mannequins GET    /:locale/mannequins(.:format)

{:controller=>“mannequins”, :action=>“update”}
recipe GET /:locale/recipes/:id(.:format)
Att.

Do yo have this?

<td><%=h recipe.author %></td>

Att.

Sorry are you iterating in a variable named @recipes?
Can you try this:

<%= link_to 'Show', recipe %> SKYPE: paulodemoc >> things >> ActionController::Routing::Routes.draw do |map| >> # Keep in mind you can assign values other than :controller >> >> # products.resources :comments >> # end >> # consider removing or commenting them out if you're using named >> ti.... >> [email protected] > [email protected] [email protected]


.
.
“Caminar sobre el agua y desarrollar software a partir de unas
For more options, visit this group at
For more options, visit this group at
especificaciones es fácil. si ambas están congeladas.”
http://groups.google.com/group/rails-i18n?hl=en.
http://groups.google.com/group/rails-i18n?hl=en.


Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

Just for you guys to check:

Routes.rb
http://pastie.org/911390

http://pastie.org/911390Application_Controller.rb
http://pastie.org/911392

If i should put any other files, lemme know…

Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

Try this:
<%= link_to ‘Show’, recipe_url(:id => recipe) %>

2010/4/9 Paulo H. [email protected]

Att.

something’s wrong with your routes - show it too us. And for the love

+55 38 9141 5400

SKYPE: paulodemoc

routes’, I got this:
{:controller=>“mannequins”, :action=>“show”}
edit_recipe GET /:locale/recipes/:id/edit(.:format)
/:controller/:action/:id(.:format)
2010/4/9 Andrés gutiérrez [email protected]

Ah! in the console put this:

But I got the same error.... 2010/4/9 Andrés gutiérrez > [email protected]> escribió: >> Line #15 contains the following: >>> MSN: [email protected] >>>> >>>>> Here is my routes.rb: >>>>> # Sample of regular route: >>>>> product.id) >>>>> >>>>> >>>>> remember to delete public/index.html. >>>>> routes and resources. >>>>> recipe_url failed to generate from {:locale=>#>>>> [email protected]. >>>> To post to this group, send email to >>> Google G. "rails-i18n" group. >> > > You received this message because you are subscribed to the Google You received this message because you are subscribed to the Google

Groups “rails-i18n” group.
Groups “rails-i18n” group.

To post to this group, send email to [email protected].
To post to this group, send email to [email protected].
Experiencia es lo que obtienes, cuando no obtienes lo que quieres.
To unsubscribe from this group, send email to
To unsubscribe from this group, send email to
To unsubscribe from this group, send email to
To unsubscribe from this group, send email to
[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rails-i18n?hl=en.


Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

It worked =]

I changed

<% @recipes.each do |recipe| %>

<td><%=h recipe.title %></td>

<td><%=h recipe.author %></td>

<td><%=h recipe.description %></td>

<td><%= link_to 'Show', *recipe_url(recipe)* %></td>

<td><%= link_to 'Edit', *edit_recipe_path(recipe)* %></td>

<td><%= link_to 'Destroy', *recipe*, :confirm => 'Are you sure?',

:method => :delete %>

<% end %>

to

<% @recipes.each do |recipe| %>

<td><%=h recipe.title %></td>

<td><%=h recipe.author %></td>

<td><%=h recipe.description %></td>

<td><%= link_to 'Show', *recipe_url(:id => recipe)* %></td>

<td><%= link_to 'Edit', *edit_recipe_path(:id => recipe)* %></td>

<td><%= link_to 'Destroy', *:id => recipe*, :confirm => 'Are you 

sure?',

:method => :delete %>

<% end %>

Now I should make the same kind of modifications on the other views…

Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

2010/4/9 Andrés gutiérrez [email protected]

something’s wrong with your routes - show it too us. And for the love
of…
don’t paste a screenshot, use http://pastie.org/ :slight_smile:

In your position i’ll try this:

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. If this should
be a problem, have a look at two plugins which simplify work with routes in
this way: Sven F.’s routing_filter [1] and Raul M.’s
translate_routes [2].

This text is from rails Guides [3]

[1] GitHub - svenfuchs/routing-filter: routing-filter wraps around the complex beast that the Rails routing system is, allowing for unseen flexibility and power in Rails URL recognition and generation.
[2] GitHub - raul/translate_routes: Rails plugin to translate your URLs and routing helpers on an clean way.
[3]
Rails Internationalization (I18n) API — Ruby on Rails Guides

El 9 de abril de 2010 15:28, Andrés gutiérrez
[email protected]escribió:

<td><%= link_to 'Show', *recipe_url(recipe)* %></td>

<td><%=h recipe.description %></td>

<% end %>
SKYPE: paulodemoc

Att.

something’s wrong with your routes - show it too us. And for the love

+55 38 9141 5400

GTALK: [email protected]

I have that on application_controller.rb, and when I ran 'rake
mannequin GET /:locale/mannequins/:id(.:format)
{:controller=>“recipes”, :action=>“new”}
/:controller/:action/:id

<% @recipes.each do |recipe| %>

El 9 de abril de 2010 13:29, Andrés gutiérrez <

+55 38 9141 5400

[email protected]> wrote:

about the :path_prefix on routes.rb. But it just didn’t
highest priority.

map.purchase ‘products/:id/purchase’, :controller =>

map.resources :products, :member => { :short => :get,

products.resources :sales, :collection => { :recent

controller
ActionController::RoutingError in Recipes#index
You received this message because you are subscribed to the

Edward V. Berard, ingeniero informático.
especificaciones es fácil. si ambas están congeladas."
For more options, visit this group at
.


[email protected][email protected]
[email protected][email protected]


.
.
“Caminar sobre el agua y desarrollar software a partir de unas
For more options, visit this group at
For more options, visit this group at
For more options, visit this group at
For more options, visit this group at
especificaciones es fácil. si ambas están congeladas.”
http://groups.google.com/group/rails-i18n?hl=en.
http://groups.google.com/group/rails-i18n?hl=en.
Edward V. Berard, ingeniero informático.


Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

It worked. Now I found another problem… the “Destroy” link doesn’t
work.
It doesn’t even prompt anymore, it just refreshes the index page and all
the
recipes are still there…
The link is being generated by this:

<%= link_to ‘Destroy’, :id => recipe, :confirm => ‘Are you sure?’,
:method => :delete %>

Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

2010/4/9 Andrés gutiérrez [email protected]

The solution of :id => whatever dosn’t the Rails Way IMHO. You should
try to
use the rails helpers (link_to, form_for,…) in the correct way:

<%= link_to “Show”, recipe %>

To solve your problems with routes i suggess you to use some of the
solutions i recomend you in the other mail.

If you prefer my “bad” solutions to make the destroy link work, try
this:

<%= link_to ‘Destroy’, recipe_path(:id => recipe) , :confirm => ‘Are you
sure?’, :method => :delete* *%>

2010/4/9 Paulo H. [email protected]

Paulo H. Vieira Neves

f| %>
http://groups.google.com/group/rails-i18n?hl=en.


Experiencia es lo que obtienes, cuando no obtienes lo que quieres.

It worked too…
Now I am having the same error on these lines:

<% form_for(@recipe) do |f| %>
<%= f.error_messages %>

=/

Att.
Paulo H. Vieira Neves
Bsc. Sistemas de Informação
+55 38 9141 5400
MSN: [email protected]
GTALK: [email protected]
SKYPE: paulodemoc

2010/4/9 Andrés gutiérrez [email protected]