Ruby on Rails. Unicode routes
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
Rails 3: Umlauts (or other unicode) in routes
Use your first idea with CGI.escape("bonitätsauskunft").downcase
It will work with any browser. It don't depend on browser encoding, but SERVER encoding. When you use another server (webrick), routes could need to be changed.
I'm using this type of "unicode" routes successfully for some time and it really has good SEO effect.
Unicode routes.rb and Passenger vs. Mongrel/WEBrick Options
This is a known passenger issue.
To work around it, you need to use the following:
match "記事" => "articles#index"
match "記事/:id" => "articles#show"
resources :articles, :path => Rack::Utils.escape("記事")
This will ensure articles_path will generate the escaped routes, while still responding to the unescaped passenger ones.
Unicode issues with acts_as_taggable_on_steroids
See ToASCII and ToUnicode in this Wikipedia article. I hope the article has enough pointers to resolve your question.
Edit: Though it talks Python, Unicode and permalinks can give an idea about how to encode a solution to your question. To summarize:
Basically, the Unicode URL is encoded in UTF8 and each byte of the UTF8-encoded string is encoded using percent encoding. The browser apparently recognized this specific encoding scheme (which isn’t documented anywhere I could fine) and displays nice internationalized URLs for the user.
Sorry, I've no idea if Rails has a ready-made function to encode URLs this way.
Rails routes with specific character
From Uniform Resource Locators (URL)
The character "#" is unsafe and should always be encoded because it is
used in World Wide Web and in other systems to delimit a URL from a
fragment/anchor identifier that might follow it.
You can't use #
symbol in your url because it's a reserved character.
You might have seen the usage of symbol like this
https://www.google.com/search?q=hello+world#brs
But this symbol used here as fragment delimeter.
Rails Routes - Id and name with spaces replaced with hyphens
The strategy here is to treat the name in the URL like a throw-away variable, since it's not being used as an identifier. Notice that in StackOverflow you can type any name you want, and as long as the id is correct you'll get the page you want. So we can do that too:
- Define the normal routes using
resources :clients
so you can go to/clients/1
- Define a another route
get 'clients/:id/:name', to: 'clients#show', as: :client_name
. This lets you go to/clients/1/anything-you-want
and it will hit theshow
action inClientsController
withid: 1
andname: "anything-you-want"
. - Define a method to generate the url-name from the user's name. For simplicity I'll assume you have defined
User#url_name
, but it might make more sense to defineurl_name(client)
in a helper since it's not really relevant to the model. - In your app, use
client_name_path(@client.id, @client.url_name)
; e.g.,client_name_path(1, "john-doe")
. This url-helper name is defined by our choice to useas: :client_name
in the route. The return value of this call should be "/clients/1/john-doe", which we have already routed to theClientsController#show
action.
So far, links in your app will have the correct URL, but a user could still type in any name in the URL and it would persist in the address bar (even though the page would be fine). If you want to go further like StackOverflow and actually change the url to the correct name you might try in ClientsController#show
, to redirect_to client_name_path(@client.id, @client.url_name) if params[:name] != @client.url_name
. Just be careful with that to not get yourself into an infinite loop.
Related Topics
Xpath Expression for Regex-Like Matching
Heroku: Gemfile.Lock Is Required Issue
Ruby - Send Get Request with Headers
Undefined Method 'Model_Name' for Project:Class
Using $ Sudo Bundle Exec ... Raises 'Bundle: Command Not Found' Error
Directly Accessing an Instance Variable VS. Using an Accessor Method
Rails - Rspec - Difference Between "Let" and "Let!"
Rails 4 Nested Attributes Not Saving
Detect If Application Was Started as Http Server or Not (Rake Task, Rconsole etc)
Ruby Gem Dependencies on Offline Server
Ruby 1.9: Regular Expressions with Unknown Input Encoding
Why Can't I Use an Integer as a Key Using the New Ruby 1.9.2 Hash Syntax
How to Match Full Words and Not Substrings in Ruby