Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails. Unicode routes

Is it possible to set a Unicode string as a segment of a path in Rails?

I try the following:

  
# app/controllers/magazines_controller.rb

class MagazinesController < ApplicationController
  def index                                     
  end                                                                           
end
  
  
# encoding: utf-8
# config/routes.rb

PublishingHouse::Application.routes.draw do
  resources :magazines,
    :only => :index,
    :path => :журналы # a Unicode string is set as a segment of the path
end
  
$ rake routes
magazines GET /журналы(.:format) {:action=>"index", :controller=>"magazines"}

But when I go to the path I get the Routing error:

$ w3m http://localhost:3000/журналы
...

Routing Error

No route matches "/%D0%B6%D1%83%D1%80%D0%BD%D0%B0%D0%BB%D1%8B"

Here's the server log:

$ rails s thin
...

Started GET "/%D0%B6%D1%83%D1%80%D0%BD%D0%B0%D0%BB%D1%8B" for 127.0.0.1 at 2010-09-26 13:35:00 +0400

ActionController::RoutingError (No route matches "/%D0%B6%D1%83%D1%80%D0%BD%D0%B0%D0%BB%D1%8B"):

Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.2ms)

Thanks.

Debian GNU/Linux 5.0.6;

Ruby 1.9.2;

Ruby on Rails 3.0.0.

like image 609
Shamaoke Avatar asked Sep 26 '10 10:09

Shamaoke


People also ask

How do I see all routes in Rails?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

What is routes in Ruby on Rails?

The routing module provides URL rewriting in native Ruby. It's a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules. Best of all, Rails' Routing works with any web server.


1 Answers

Intereting, I think Rails need a patch for this. I shall speak with someone from core about it later. In the meantime, the following should work:

PublishingHouse::Application.routes.draw do
  resources :magazines,
    :only => :index,
    :path => Rack::Utils.escape('журналы') # a Unicode string is set as a segment of the path
end
like image 149
raggi Avatar answered Sep 19 '22 10:09

raggi