设计:多个登录页面...如何将验证失败重定向到适当的页面

时间:2015-01-19 00:18:01

标签: ruby-on-rails authentication ruby-on-rails-4 devise

所以我有我的Rails4应用程序和Devise,我有一个正常的登录/注册页面,然后我在我的结账时有一个登录/注册页面。

结帐注册/登录重定向,如果没有任何验证失败,我需要它。当我的结账注册或结账登录页面上的验证失败时,它会重定向到默认路径。

如何在结帐登录/注册页面上显示验证失败?

在我的应用程序控制器中:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  after_filter :store_location

  private 

    def store_location
      # store last url - this is needed for post-login redirect to whatever the user last visited.
      return unless request.get? 
      if (request.path != "/account/sign_in" &&
          request.path != "/account/sign_up" &&
          request.path != "/account/password/new" &&
          request.path != "/account/password/edit" &&
          request.path != "/account/confirmation" &&
          request.path != "/account/sign_out" &&
          !request.fullpath.match("/account") && #for confirmable
          !request.xhr?) # don't store ajax calls
        session[:previous_url] = request.fullpath 
      end
      #FOR CHECKOUT
      if request.path == "/checkout/sign_in"
        session[:previous_url] = "/checkout"
        #STORE LOGIN PATH
        session[:login_url] = request.fullpath
      end
    end

    def after_sign_in_path_for(resource)
      session[:previous_url] || root_path
    end

    ...

end

这是我的注册覆盖,我试图使用我的会话[:login_url]指向正确的登录页面。

此行不起作用 respond_with资源,位置:after_sign_up_path_for(资源),但正确使用会话变量设置位置。

class RegistrationsController < Devise::RegistrationsController


  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      #THIS DOES NOT WORK, EVEN THOUGH LOCATION IS SET PROPERLY
      respond_with resource, location: after_sign_up_path_for(resource)
    end
  end

  private

    def after_sign_up_path_for(resource)
      session[:login_url] || new_user_registrations_path
    end


end

1 个答案:

答案 0 :(得分:0)

如果我是你,我会添加一个url参数,例如?ret = yourpath。然后你可以使用相同的登录路径,如果存在该param,则返回默认值。您也可以通过为您的结帐登录子类化Devise :: SessionsController并覆盖验证失败的方法来完成此操作。我会提供链接,但我在手机上。它可以在主要的自述文件中找到。