提交 b2c2b0ce 编写于 作者: J José Valim

Rails router automatically calculated for you the controller and named routes...

Rails router automatically calculated for you the controller and named routes in the following scenarios:

  match "home/about"                 #=> maps to home#about with named route home_about_path
  match "about"                      #=> does not work because it cannot guess the controller
  match "about" => "home#about"      #=> maps to home#about with named route home_about_path
  match "home/about", :as => "about" #=> maps to home#about with named route about_path
上级 f5ee855f
......@@ -55,10 +55,8 @@ def extract_path_and_options(args)
path = normalize_path(path)
if using_match_shorthand?(path, options)
options = {
:to => path[1..-1].sub(%r{/([^/]*)$}, '#\1'),
:as => path[1..-1].gsub("/", "_")
}.merge!(options)
options[:to] ||= path[1..-1].sub(%r{/([^/]*)$}, '#\1')
options[:as] ||= path[1..-1].gsub("/", "_")
end
[ path, options ]
......@@ -71,7 +69,7 @@ def using_to_shorthand?(args, options)
# match "account/overview"
def using_match_shorthand?(path, options)
path && options.except(:via, :anchor).empty? && !path.include?(':')
path && options.except(:via, :anchor, :to, :as).empty? && path =~ %r{^/[\w\/]+$}
end
def normalize_path(path)
......
......@@ -18,10 +18,9 @@ def self.matches?(request)
default_url_options :host => "rubyonrails.org"
controller :sessions do
get 'login' => :new, :as => :login
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy, :as => :logout
delete 'logout' => :destroy
end
resource :session do
......@@ -35,6 +34,7 @@ def self.matches?(request)
match 'account/overview'
match '/account/nested/overview'
match 'sign_in' => "sessions#new"
match 'account/modulo/:name', :to => redirect("/%{name}s")
match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" }
......@@ -673,6 +673,14 @@ def test_convention_match_nested_and_with_leading_slash
end
end
def test_convention_with_explicit_end
with_test_routes do
get '/sign_in'
assert_equal 'sessions#new', @response.body
assert_equal '/sign_in', sign_in_path
end
end
def test_redirect_with_complete_url
with_test_routes do
get '/account/google'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册