繁体   English   中英

Rails 5 Devise:更改失败的注册重定向路径

[英]Rails 5 Devise: Change Unsuccessful registration redirect path

我正在尝试创建一个具有目标网页和目标网页本身上的用户注册表格的简单网站。

当注册过程中出现表单错误(例如缺少字段)时,我很难弄清楚如何更改devise注册控制器以重定向回登录页面(根路径“ /”)。

现在,如果成功注册,该表格将正常运行。 但是,如果注册中存在错误,它将重定向到仅包含注册表单的/users/sign_up视图。 如果注册期间发生错误,我需要它来重定向/渲染目标页面。

我已经生成了一个自定义注册控制器,但是现在它仅调用super。

# POST /resource
def create
  super
end

我也看过after_sign_up_path_for ,但是那行不通,因为仅在成功注册后才调用该路径。

考虑到这种设计模式可能非常普遍,我怀疑该解决方案可能是一个简单的解决方案,但是我不知道该怎么做。

您可以从此处复制设备注册控制器

您应该能够添加如下内容:

应用程序/控制器/ registrations_controller.rb

   class RegistrationsController < Devise::RegistrationsController
      def new
        super
      end

      def create
        build_resource(sign_up_params)
        if resource.save
          #respond_with resource, location: root_path
          return
        else
          #respond_with resource, location: lending_page_path
          return
        end
        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
          respond_with resource
         end
      end
  end

然后在您的路线中:

devise_for :users, :controllers => {:registrations => 'registrations'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM