簡體   English   中英

如果會話登錄錯誤,Rails會設計,然后重定向到某個頁面(返回)

[英]Rails devise if session sign_in error, then redirect to some page (back)

我該如何更改standart devise session_controller,以便例如,如果我輸入了錯誤的密碼,我將被重定向:back(不是必需的),並查看我的錯誤...

我需要這個嗎? 因為我只有在模板中登錄,該模板在所有頁面的布局左圖塊中呈現,所以我沒有新的動作視圖...

那么如何將錯誤重定向到帶有驗證消息錯誤的其他頁面...?

這是標准的session_controller(現在我沒有創建它,它是“嵌套的”):

class Devise::SessionsController < DeviseController
  prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
  prepend_before_filter :allow_params_authentication!, :only => :create
  prepend_before_filter { request.env["devise.skip_timeout"] = true }

  # GET /resource/sign_in
  def new
    self.resource = build_resource(nil, :unsafe => true)
    clean_up_passwords(resource)
    respond_with(resource, serialize_options(resource))
  end

  # POST /resource/sign_in
  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, resource)
    respond_with resource, :location => after_sign_in_path_for(resource)
  end

  # DELETE /resource/sign_out
  def destroy
    redirect_path = after_sign_out_path_for(resource_name)
    signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
    set_flash_message :notice, :signed_out if signed_out && is_navigational_format?

    # We actually need to hardcode this as Rails default responder doesn't
    # support returning empty response on GET request
    respond_to do |format|
      format.all { head :no_content }
      format.any(*navigational_formats) { redirect_to redirect_path }
    end
  end

  protected

  def serialize_options(resource)
    methods = resource_class.authentication_keys.dup
    methods = methods.keys if methods.is_a?(Hash)
    methods << :password if resource.respond_to?(:password)
    { :methods => methods, :only => [:password] }
  end

  def auth_options
    { :scope => resource_name, :recall => "#{controller_path}#new" }
  end
end

appcontroller中的方法

def after_sign_in_path_for(resource)      
    case resource
      when User then 
        user = User.find(current_user.id)
        total_markup_for_user = user.groups.map(&:markup).sum
        if current_cart.present?
          cart_id = current_cart.id
        else
          cart_id = nil
        end
        ***
        end
        if request.referrer.to_s.include? "users"
          root_path
        else
          request.referrer 
        end
      when Admin::Admin then :admin_mainpage
    end
  end

Devise的文檔向您展示了如何在此處覆蓋其默認重定向:

https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in

基本上,您將進入您的application_controller.rb並例如編寫如下內容

def after_sign_in_path_for(resource)
  request.env['omniauth.origin'] || stored_location_for(resource) || new_survey_path
end

def after_sign_out_path_for(resource_or_scope)
 root_path
end

結束

希望能幫助到你

嘗試類似這樣的操作,添加一個新的response_with,以防sign_in返回false。

def create
  self.resource = warden.authenticate!(auth_options)
  set_flash_message(:notice, :signed_in) if is_navigational_format?
  if sign_in(resource_name, resource)
    respond_with resource, location: after_sign_in_path_for(resource)
  else
    respond_with resource, location: root_path
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM