3
2

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.

redirect_toメソッド

Last updated at Posted at 2017-02-26

##redirect_to "'http://qiita.com':リダイレクト先のURL", :status => :'moved_permanently:ステータスコード'

basic_controller.rb(res_redirectアクション)
#外部サイトにリダイレクト(301 Moved Permanentlyでアドレス移転を通知)
redirect_to 'http://qiita.com', :status => :moved_permanently
#同じコントローラないの異なるアクションにリダイレクト
redirect_to :action => 'render_text'
#articlesコントローラのindexアクションにリダイレクト
redirect_to :controller => 'articles', :action => 'index'
#リダイレクト先を自動生成されるURLヘルパーで指定
redirect_to article_path(1)
#一つ前のページにリダイレクト
redirect_to :back

redirect_to('{ :action => 'redirect_flash2' }:リダイレクト先のURL',
:notice => 'noticeメッセージ:リダイレクト先に引き渡したいメッセージ')
}
redirect_to('{ :action => 'redirect_flash2' }:リダイレクト先のURL',
:flash => {':notice:パラメータ名' => 'noticeメッセージ:リダイレクト先に引き渡したいメッセージ' })

basic_controller.rb(redirect_flashアクション)
#redirect_flash2アクションにflashメッセージnoticeを引き渡す
redirect_to({ :action => 'redirect_flash2' },
 :notice => 'noticeメッセージ')
#以下のコードも同じ意味
redirect_to({ :action => 'redirect_flash2' },
 :flash => {:notice => 'noticeメッセージ' })
basic/redirect_flash2.html.erb
<%# いずれも同じ意味(flash[...]の気泡は、notice、alert以外で利用) %>
<p><%= notice %></p>
<p><%= flash[:notice] %></p>
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?