Ruby on rails 设计:注册失败时重定向?
我正在尝试重定向未通过注册表单的用户(例如,他们输入了一个已使用的用户名,他们将一个字段留空,等等) 我为登录表单失败的用户设置了自定义失败,代码如下:Ruby on rails 设计:注册失败时重定向?,ruby-on-rails,ruby,ruby-on-rails-3,rubygems,devise,Ruby On Rails,Ruby,Ruby On Rails 3,Rubygems,Devise,我正在尝试重定向未通过注册表单的用户(例如,他们输入了一个已使用的用户名,他们将一个字段留空,等等) 我为登录表单失败的用户设置了自定义失败,代码如下: class CustomFailure < Devise::FailureApp def redirect_url root_path end def respond if http_auth? http_auth else redirect
class CustomFailure < Devise::FailureApp
def redirect_url
root_path
end
def respond
if http_auth?
http_auth
else
redirect
end
end
class CustomFailure
然而,我一直被困在如何设置注册失败。理想情况下,我只想将它们重定向回根路径,有什么想法吗?谢谢大家! 您可能需要子类化
设计::RegistrationController
并覆盖创建操作。只需从中复制create方法,并在保存失败时修改重定向即可
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
# modify logic to redirect to root url
end
end
提示:
要保留flash错误消息,请在覆盖中重定向到之前添加此行
resource.errors.full_messages.each {|x| flash[x] = x}
因此,在您的注册_controller.rb中:
def create
build_resource(sign_up_params)
if resource.save
yield resource if block_given?
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
clean_up_passwords resource
resource.errors.full_messages.each {|x| flash[x] = x} # Rails 4 simple way
redirect_to root_path
end
end
修改Desive的某些部分以满足您的需要有点繁琐,我怀疑这是因为gem在覆盖大多数常见情况方面做得很好。然而,使用Desive的边缘案例很多,您的问题指向其中一个。我必须做一些类似的事情,即当用户执行以下操作之一时,确保设计重定向到特定页面:
class RegistrationsController < Devise::RegistrationsController
end
类注册控制器
在该控制器内,您将覆盖Desive中的create方法。转到Desive github页面,查看create方法并复制该方法中的代码。然后创建一个私有方法来覆盖if语句最后一个块的返回语句。你的控制器应该是这样的
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
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}"
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
response_to_sign_up_failure resource
end
end
private
def response_to_sign_up_failure(resource)
if resource.email == "" && resource.password == nil
redirect_to root_path, alert: "Please fill in the form"
elsif User.pluck(:email).include? resource.email
redirect_to root_path, alert: "email already exists"
end
end
end
类注册控制器
它应该可以工作。在config/routes.rb中:
devise_scope :user do
get '/users', to: 'devise/registrations#new'
end
很好!非常感谢你的帮助!我喜欢这个主意,这是迄今为止我发现的最好的主意。但它呈现了一个空的形式;您是否找到了一种方法,可以在输入仍然归档的情况下,在每个错误字段下返回一条错误消息?
devise_scope :user do
get '/users', to: 'devise/registrations#new'
end