gpt4 book ai didi

ruby-on-rails - Rails 设计如果 session 登录错误,则重定向到某个页面(返回)

转载 作者:行者123 更新时间:2023-12-02 05:12:17 25 4
gpt4 key购买 nike

我如何更改标准设计 session_controller,这样,如果我输入了错误的密码,我将被重定向 :back(不需要),并查看我的错误...

我需要这个做什么?因为我只在模板中登录,该模板在所有页面的布局左侧 block 中呈现,而且我没有新的操作 View ...

那么如何将错误重定向到具有验证消息错误的其他页面...?

这是标准的 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

结束

希望对你有帮助

关于ruby-on-rails - Rails 设计如果 session 登录错误,则重定向到某个页面(返回),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15248854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com