1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

matchメソッド ルーティング

Last updated at Posted at 2017-02-25

##match '/news/:content_id:URLパターン' => 'news#show:ルート先(コントローラ名#アクション名)'

routes.rb
match '/news/:content_id' => 'news#show'
	/new/:content_id(.:format) {:controller=>"news", :action=>"show"}
	. /news/aspnet01news/108
	match 'hello/intro'
	hello_intro/hello/intro(.:format)
	{:controller=>"hello", :action=>"intro"}
	. /hello/intro
match '/products/*keywords/:product_id' => 'producr#show'
	/products/*keywords/:product_id(.:format) 
	{:controller=>"products", :action=>"show"}
	. /products/ruby/rails/wings/development/108
match ':controller(/:action(/:id(.:format)))'
	/:controller(/:action(/:id(.:format))) 
	例. /hello /model/select /books/show/108 /blogs/show/1.xml

##match 'hello/intro:URLパターン', :via => :'get:HTTPメソッド'

routes.rb
match 'hello/intro', :via => :get
	hello_intro GET /hello/intro(.:format)
		{:controller=>"hello", :action=>"intro"}
get 'hello/intro'
	hello_intro GET /hello/intro(.:format)
		{:controller=>"hello", :action=>"intro"}
match 'hello/process', :via => [:get, :post]
	hello_intro GET/POST /hello/intro(.:format)
		{:controller=>"hello", :action=>"process"}

##match '/new/content_id:URLパターン' => redirect('/articles/%{content_id}:リダイレクト先URLパターン')
match '/news/:content_id:URLパターン' =>
redirect {|'p:パラメータ情報', 'req:リクエスト情報'| "/articles/#{p[:content_id].to_i + 1000}:リダイレクト先のを構築する式"}

routes.rb
match '/new/content_id' => redirect('/articles/%{content_id}')
match '/news/:content_id' =>
	redirect {|p, req| "/articles/#{p[:content_id].to_i + 1000}"}

##match '/news(/:content_id):URLパターン', => 'news#show:ルート先'
:defaults => [ ':content_id:パラメータ名' => '1:デフォルト値']

routes.rb
match '/news(/:content_id)', => 'news#show'
	:defaults => [ :content_id => 1, :format => :json]
	/news(:content_id)(.:format)
	(:content_id=>1, :format=>;json, :controller=>"news", :action=>"show")

##match '/books:URLパターン' => 'books#list:ルート先', :as => ':article:ヘルパー名'

routes.rb
#「/books」に対応するUrlヘルパーはarticle_url,article_path
match '/books' => 'books#list', :as => :article
	article /books(.:format) {:controller=>"books", :action+>"list"}

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?