登录后尝试“redirect_to:back”

时间:2015-07-31 17:16:45

标签: ruby-on-rails twitter-bootstrap ruby-on-rails-4 redirecttoaction

我正在尝试在用户登录后重定向到上一页。我使用了一个用于登录/注册表单的引导模式但是如果某人没有在他们的浏览器上启用JS并被带到'/ login '页面,我想确保将它们重定向到根网址。我知道current_page?不适用于POST请求。

我已经尝试了很多东西,所以下面的代码正在从'/ login'页面正确地重定向到根URL,但是在使用bootstrap模式登录时我没有被重定向到':back'。

这是来自SessionsController :( PS-我有会话#new / sessions#create as / login in routes)

def create
    user = User.find_by(email: params[:email]) 
    if user && user.authenticate(params[:password])
        session[:user_id] = user.id

            if request.path === '/login'
                redirect_to '/'
            else
                redirect_to :back
            end

            flash[:success] = "Logged in."

    else
        flash.now[:danger] = "Email and password did not match. Please try again."
        render :new
    end
end

def destroy
    session[:user_id] = nil
    flash[:success] = "Logged out."
    redirect_to '/'
end

routes.rb中:

Rails.application.routes.draw do

root to: 'home#home'

resources :users

get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'

end

2 个答案:

答案 0 :(得分:0)

Try this and let me know if it works:

Routes.rb

Rails.application.routes.draw do

root to: 'home#home'

resources :users

get  '/login',  to: 'sessions#new',    as: 'login' 
post '/login',  to: 'sessions#create'
get  '/logout', to: 'sessions#destroy'

end

Controller.rb

def create
    user = User.find_by(email: params[:email]) 
    if user && user.authenticate(params[:password])
        session[:user_id] = user.id

            if request.path === '/login'
                redirect_to '/'
            else
                redirect_to login_url
                flash[:notice] = "Incorrect information"
            end

            flash[:success] = "Logged in."

    else
        flash.now[:danger] = "Email and password did not match. Please try again."
        render :new
    end
end

def destroy
    session[:user_id] = nil
    flash[:success] = "Logged out."
    redirect_to '/'
end

I created something like a while back. Check this out, it might help you later. https://github.com/Mukul215/micropost_app/blob/master/app/controllers/sessions_controller.rb

答案 1 :(得分:0)

好吧我明白了。

的routes.rb

Rails.application.routes.draw do

root to: 'home#home'

resources :users

get '/login', to: 'sessions#new', as: 'login'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'

end

SessionsController.rb

class SessionsController < ApplicationController

def new
end

def create
    user = User.find_by(email: params[:email]) 
    if user && user.authenticate(params[:password])
        session[:user_id] = user.id

            if request.referrer == login_url
                redirect_to root_path
            else
                redirect_to :back
            end

            flash[:success] = "Logged in."

    else
        flash.now[:danger] = "Email and password did not match. Please try again."
        render :new
    end
end

def destroy
    session[:user_id] = nil
    flash[:success] = "Logged out."
    redirect_to root_path
end

end

因此,如果用户在login_url以外的任何其他页面上使用Bootstrap Modal登录,则会将其重定向回当前页面。如果他们从实际登录页面登录,则会将其重定向到root_path