Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support unicode character route directly in config/routes.rb. Fix #3470. #6646

Merged
merged 1 commit into from

5 participants

@kennyj
Collaborator

Support unicode character route in config/routes.rb, e.g.:

get 'こんにちは', :controller => 'home', :action => 'index'

In current Rails, we had to draw encoded route, e.g.:

get Rack::Utils.escape('こんにちは'), :controller => 'home', :action => 'index'

Please see also #3470

@steveklabnik
Collaborator

Mentioning #3470 so GitHub links them.

@kennyj
Collaborator

@steveklabnik
thanks !
I should use #xxxx instead of url.

@steveklabnik
Collaborator

Yep! You can also link to another repo's issue by user/repo#number

@drogus
Collaborator

@kennyj this breaks backwards compatibility because:

#encoding: utf-8
require 'rack/utils'
p Rack::Utils.escape('こんにちは')
#=> "%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF"
p Rack::Utils.escape(Rack::Utils.escape('こんにちは')) 
#=> "%25E3%2581%2593%25E3%2582%2593%25E3%2581%25AB%25E3%2581%25A1%25E3%2581%25AF"

If someone escapes paths already, their rotes will break. I'm not saying that this is the thing that prevents us from merging it, just pointing out the problem. Maybe an addition to upgrading guide will be sufficient.

@kennyj
Collaborator

@drogus I knew it, and I think my PR is proposal one.

I'm going to add an entry on https://github.com/rails/rails/blob/master/guides/source/upgrading_ruby_on_rails.textile.

@kennyj
Collaborator

done !

Since my English is poor, If my entry is not good English, please tell me ;0

@kuraga

@kennyj Thank you! Should we write something for API-docs? My English is too bad :)

@kennyj
Collaborator

Should we add an entry on http://guides.rubyonrails.org/routing.html ?
Are there any other ?

@kennyj
Collaborator

@drogus @kuraga I added some docs.

actionpack/CHANGELOG.md
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ##
+* Support unicode character route in config/routes.rb, e.g.:

I don't think it's necessary to talk about "config/routes.rb".. Only routes should suffice. How about showing the old way before?

Support unicode characters in routes, so instead of manually escaping:

    get Rack::Utils.escape('こんにちは') => 'home#index'

It's possible to write the unicode route and Rails will automatically escape it:

    get 'こんにちは' => 'home#index'
@kuraga
kuraga added a note

Yes, you're right.

@kuraga
kuraga added a note

What do you think about an idea to highlight that the argument will be automatically escaped (see #6646 (comment); how to insert comment's link?) This changelog means that I may don't escape unicode characters. But I must don't do it...

Yeah sounds good. Can remove the It's possible to write the unicode route to something like Just write the unicode route, or similar :).

@kuraga
kuraga added a note
Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:

    get Rack::Utils.escape('こんにちは') => 'home#index'

You just have to write the unicode route:

    get 'こんにちは' => 'home#index'

Perfect :green_heart:

@kennyj Collaborator
kennyj added a note

@kuraga @carlosantoniodasilva thanks !
I'm updating CHANGELOG.

p.s. こんにちは means hello in Japanese. It's safety word :D

Nice, I feel like I have to learn Japanese :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@carlosantoniodasilva

I think we're fine to have a note on the upgrading guides and the changelog, showing people they don't need to escape routes anymore manually. It's a quick change to do, in only one file, in case they are.

@kuraga

Thank you!!! I'm very glad to take part of creating first Rails' change of my life!!! I'm hope for accepting this PR. And thank you about translation of 'こんにちは' :)

@drogus
Collaborator

@kennyj sorry for delay, I looked on guides entry that you added and it looks good

@drogus
Collaborator

@kennyj or, actually, one really small thing, I would change "Rails 4.0 also changed how draws unicode character routes." to "Rails 4.0 also changed the way unicode character routes are drawn."

actionpack/CHANGELOG.md
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ##
+* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
+
+ get Rack::Utils.escape('こんにちは') => 'home#index'
@kuraga
kuraga added a note

Sorry but one more thing.
I think line of code should be highlighted by spaces by left?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
actionpack/CHANGELOG.md
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ##
+* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
+
+ get Rack::Utils.escape('こんにちは') => 'home#index'
+
+ You just have to write the unicode route:
+
+ get 'こんにちは' => 'home#index'
@kuraga
kuraga added a note

...And here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@kennyj
Collaborator

@drogus @kurage Thank you. I'm updating it ... done !

guides/source/upgrading_ruby_on_rails.textile
@@ -50,6 +50,8 @@ h4(#action_pack4_0). Action Pack
Rails 4.0 changed how <tt>assert_generates</tt>, <tt>assert_recognizes</tt>, and <tt>assert_routing</tt> work. Now all these assertions raise <tt>Assertion</tt> instead of <tt>ActionController::RoutingError</tt>.
+Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw these, you must change these, e.g. <tt>get Rack::Utils.escape('こんにちは'), :controller => 'welcome', :action => 'index'</tt> to <tt>get 'こんにちは', :controller => 'welcome', :action => 'index'</tt>.

I think we can just change this slightly, how about:

If you already draw these, you must change these, => If you already draw such routes, you must change them,

@kuraga
kuraga added a note

+1

@kennyj Collaborator
kennyj added a note

@carlosantoniodasilva @kuraga thanks ! I agree with you, and I updated it !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@carlosantoniodasilva carlosantoniodasilva merged commit 9abe098 into rails:master
@carlosantoniodasilva

@kennyj merged, thanks!

@kuraga

Great! Thanks!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 15, 2012
  1. @kennyj
This page is out of date. Refresh to see the latest.
View
10 actionpack/CHANGELOG.md
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ##
+* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
+
+ get Rack::Utils.escape('こんにちは') => 'home#index'
+
+ You just have to write the unicode route:
+
+ get 'こんにちは' => 'home#index'
+
+ *kennyj*
+
* Return proper format on exceptions. *Santiago Pastorino*
* Allow to use mounted_helpers (helpers for accessing mounted engines) in ActionView::TestCase. *Piotr Sarnacki*
View
7 actionpack/lib/action_dispatch/routing.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
require 'active_support/core_ext/object/to_param'
require 'active_support/core_ext/regexp'
@@ -218,6 +219,12 @@ module ActionDispatch
#
# match "/stories" => redirect("/posts")
#
+ # == Unicode character routes
+ #
+ # You can specify unicode character routes in your router:
+ #
+ # match "こんにちは" => "welcome#index"
+ #
# == Routing to Rack Applications
#
# Instead of a String, like <tt>posts#index</tt>, which corresponds to the
View
2  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -1387,7 +1387,7 @@ def add_route(action, options) # :nodoc:
options[:as] = name_for_action(options[:as], action)
end
- mapping = Mapping.new(@set, @scope, path, options)
+ mapping = Mapping.new(@set, @scope, URI.parser.escape(path), options)
app, conditions, requirements, defaults, as, anchor = mapping.to_route
@set.add_route(app, conditions, requirements, defaults, as, anchor)
end
View
5 actionpack/test/controller/routing_test.rb
@@ -1760,6 +1760,7 @@ class RackMountIntegrationTests < ActiveSupport::TestCase
get 'account(/:action)' => "account#subscription"
get 'pages/:page_id/:controller(/:action(/:id))'
get ':controller/ping', :action => 'ping'
+ get 'こんにちは/世界', :controller => 'news', :action => 'index'
match ':controller(/:action(/:id))(.:format)', :via => :all
root :to => "news#index"
}
@@ -1876,6 +1877,10 @@ def test_extras
assert_equal({:controller => 'people', :action => 'create', :person => { :name => 'Josh'}}, params)
end
+ def test_unicode_path
+ assert_equal({:controller => 'news', :action => 'index'}, @routes.recognize_path(URI.parser.escape('こんにちは/世界'), :method => :get))
+ end
+
private
def sort_extras!(extras)
if extras.length == 2
View
4 actionpack/test/dispatch/routing_test.rb
@@ -2472,7 +2472,7 @@ def app; Routes end
class TestUnicodePaths < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
- get "/#{Rack::Utils.escape("ほげ")}" => lambda { |env|
+ get "/ほげ" => lambda { |env|
[200, { 'Content-Type' => 'text/plain' }, []]
}, :as => :unicode_path
end
@@ -2727,4 +2727,4 @@ def show
assert_response :bad_request
end
end
-end
+end
View
8 guides/source/routing.textile
@@ -644,6 +644,14 @@ You should put the +root+ route at the top of the file, because it is the most p
NOTE: The +root+ route only routes +GET+ requests to the action.
+h4. Unicode character routes
+
+You can specify unicode character routes directly. For example
+
+<ruby>
+match 'こんにちは' => 'welcome#index'
+</ruby>
+
h3. Customizing Resourceful Routes
While the default routes and helpers generated by +resources :posts+ will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers.
View
2  guides/source/upgrading_ruby_on_rails.textile
@@ -50,6 +50,8 @@ h4(#action_pack4_0). Action Pack
Rails 4.0 changed how <tt>assert_generates</tt>, <tt>assert_recognizes</tt>, and <tt>assert_routing</tt> work. Now all these assertions raise <tt>Assertion</tt> instead of <tt>ActionController::RoutingError</tt>.
+Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, e.g. <tt>get Rack::Utils.escape('こんにちは'), :controller => 'welcome', :action => 'index'</tt> to <tt>get 'こんにちは', :controller => 'welcome', :action => 'index'</tt>.
+
h4(#helpers_order). Helpers Loading Order
The loading order of helpers from more than one directory has changed in Rails 4.0. Previously, helpers from all directories were gathered and then sorted alphabetically. After upgrade to Rails 4.0 helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use <tt>helpers_path</tt> parameter, this change will only impact the way of loading helpers from engines. If you rely on the fact that particular helper from engine loads before or after another helper from application or another engine, you should check if correct methods are available after upgrade. If you would like to change order in which engines are loaded, you can use <tt>config.railties_order=</tt> method.
Something went wrong with that request. Please try again.