---
# System prepended metadata

title: '[Ruby On Rails][devise] 設定登入後轉址至特定頁面'
tags: [Ruby On Rails, devise]

---

# [Ruby On Rails][devise] 設定登入後轉址至特定頁面

###### tags: `Ruby On Rails`,`devise`


[資料來源](https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in)

<hr>

application_controller.rb
```ruby
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception


###################加上這段#######################
#home_path為網址,可自定
#也可改成"網址名"導向其他外部網址
  def after_sign_in_path_for(resource)
    stored_location_for(resource) || home_path   
  end
###################加上這段#######################
  

end
```
