mirror of
https://github.com/zhigang1992/tsemple.git
synced 2026-01-12 17:52:57 +08:00
Explicit set login/signup return_to path
This commit is contained in:
@@ -13,8 +13,7 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
def login_required
|
||||
unless login?
|
||||
store_location
|
||||
redirect_to login_url
|
||||
redirect_to login_url(return_to: (request.fullpath if request.get?))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -76,17 +75,12 @@ class ApplicationController < ActionController::Base
|
||||
@current_user ||= User.find_by_access_token(params[:access_token]) if params[:access_token]
|
||||
end
|
||||
|
||||
def store_location(path = nil)
|
||||
session[:return_to] = path || request.fullpath
|
||||
def store_location(path)
|
||||
session[:return_to] = path
|
||||
end
|
||||
|
||||
def redirect_back_or_default(default)
|
||||
redirect_to(session[:return_to] || default)
|
||||
session[:return_to] = nil
|
||||
end
|
||||
|
||||
def redirect_referrer_or_default(default)
|
||||
redirect_to(request.referrer || default)
|
||||
redirect_to(session.delete(:return_to) || default)
|
||||
end
|
||||
|
||||
def forget_me
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class SessionsController < ApplicationController
|
||||
def new
|
||||
store_location params[:return_to] if params[:return_to].present?
|
||||
store_location params[:return_to]
|
||||
end
|
||||
|
||||
def create
|
||||
|
||||
@@ -2,7 +2,7 @@ class UsersController < ApplicationController
|
||||
before_filter :no_login_required, only: [:new, :create]
|
||||
|
||||
def new
|
||||
store_location params[:return_to] if params[:return_to].present?
|
||||
store_location params[:return_to]
|
||||
@user = User.new
|
||||
end
|
||||
|
||||
|
||||
10
app/helpers/application_helper.rb
Normal file
10
app/helpers/application_helper.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
module ApplicationHelper
|
||||
def return_to_path(path)
|
||||
case path
|
||||
when '/', /^\/login/, /^\/signup/
|
||||
nil
|
||||
else
|
||||
path
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -54,10 +54,10 @@ html
|
||||
= t '.logout'
|
||||
- else
|
||||
li
|
||||
a.item href=signup_path(return_to: request.original_url)
|
||||
a.item href=signup_path(return_to: params[:return_to] || return_to_path(request.fullpath))
|
||||
= t '.sign_up'
|
||||
li
|
||||
a.item href=login_path(return_to: request.original_url)
|
||||
a.item href=login_path(return_to: params[:return_to] || return_to_path(request.fullpath))
|
||||
= t '.sign_in'
|
||||
#navbar.collapse.navbar-collapse
|
||||
ul.nav.navbar-nav
|
||||
|
||||
@@ -75,12 +75,12 @@
|
||||
- else
|
||||
= t '.your_account_had_been_locked'
|
||||
- else
|
||||
a href=signup_path(return_to: request.original_url)
|
||||
a href=signup_path(return_to: request.fullpath)
|
||||
= t '.sign_up'
|
||||
'
|
||||
= t '.or'
|
||||
'
|
||||
a href=login_path(return_to: request.original_url)
|
||||
a href=login_path(return_to: request.fullpath)
|
||||
= t '.login'
|
||||
'
|
||||
= t '.to_comment'
|
||||
|
||||
11
test/helpers/application_helper_test.rb
Normal file
11
test/helpers/application_helper_test.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ApplicationHelperTest < ActionView::TestCase
|
||||
test "return_to should return right path" do
|
||||
assert_equal nil, return_to_path('/')
|
||||
assert_equal '/?page=2', return_to_path('/?page=2')
|
||||
assert_equal nil, return_to_path('/login')
|
||||
assert_equal nil, return_to_path('/signup')
|
||||
assert_equal '/topics', return_to_path('/topics')
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user