设计:重定向注册失败?

ahu*_*ng7 13 ruby rubygems ruby-on-rails devise ruby-on-rails-3

我正在尝试重定向已注册表单失败的用户(例如,他们输入了已经使用的用户名,他们将字段留空了等等...)

我为登录表单失败的用户设置了自定义故障,代码如下:

class CustomFailure < Devise::FailureApp 

   def redirect_url 
      root_path 
   end 

   def respond 
      if http_auth? 
      http_auth 
   else 
      redirect 
   end 
 end
Run Code Online (Sandbox Code Playgroud)

但是,我一直坚持如何设置注册失败.理想情况下,我只想将它们重定向回/ root_path,任何想法?谢谢!

Dav*_*vid 19

您可能需要子类化Devise::RegistrationsController并覆盖create动作.只需从此处复制create方法,并在失败时修改重定向以进行保存.

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController


  def create
    # modify logic to redirect to root url
  end


end 
Run Code Online (Sandbox Code Playgroud)

更改路线以告知Devise使用您的控制器:

# config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
Run Code Online (Sandbox Code Playgroud)


Paa*_*Yaw 7

修改设计的某些部分以满足您的需求有点乏味,我怀疑这是因为宝石在覆盖大多数常见情况方面做得很好.然而,使用设计的边缘情况很多,你的问题指向其中一个.我必须做类似的事情,也就是说,当用户执行以下操作之一时,请确保设计重定向到特定页面:

  1. 在空表格上提交表格
  2. 提交已存在的电子邮件.以下是我处理它的方式.

首先,创建一个名为RegistrationsController的控制器,它继承自Devise :: RegistrationsController,如下所示:

 class RegistrationsController < Devise::RegistrationsController
 end
Run Code Online (Sandbox Code Playgroud)

在此控制器中,您将覆盖设计中的create方法.转到devise github页面,https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb 查看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
Run Code Online (Sandbox Code Playgroud)

它应该工作.


Doc*_*rRu 5

小费:

要保留Flash错误消息,请在覆盖中的redirect_to之前添加此行

    resource.errors.full_messages.each {|x| flash[x] = x}
Run Code Online (Sandbox Code Playgroud)

所以在你的registrations_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
Run Code Online (Sandbox Code Playgroud)