2014年3月15日 星期六

rails devise return to previous page

用devise這個gem時

當想要再登入等動作後 回到之前的那一頁

可以如此做
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update



In ApplicationController write following:
after_filter :store_location

def store_location
  # store last url - this is needed for post-login redirect to whatever the user last visited.
  if (request.fullpath != "/users/sign_in" &&
      request.fullpath != "/users/sign_up" &&
      request.fullpath != "/users/password" &&
      request.fullpath != "/users/sign_out" &&
      !request.xhr?) # don't store ajax calls
    session[:previous_url] = request.fullpath 
  end
end

def after_sign_in_path_for(resource)
  session[:previous_url] || root_path
end
如果之前已經有在 before_filter :authenticate_user! 你的ApplicationController 裡面
要把before_filter :store_location 放在更上面


沒有留言:

張貼留言