Rails 5设计:更改不成功的注册重定向路径



我正在尝试创建一个带有着陆页的简单网站,并且在着陆页本身上创建一个用户注册表格。

我很难弄清楚如何更改设计的注册控制器,以重定向到登录页面(root路径"/"(,当时在注册过程中出现形式错误(例如缺少字段(。

现在,如果有成功的注册,则表格可以正常工作。但是,如果注册中存在错误,它将其重定向到/users/sign_up视图,该视图仅包含注册表格。我需要它在注册期间发生错误时重定向/渲染着陆页。

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

# POST /resource
def create
  super
end

我也看过after_sign_up_path_for,但是这不起作用,因为该路径仅被要求成功注册。

我怀疑解决方案可能很简单,因为这种设计模式可能很普遍,但是我无法弄清楚如何做。

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

您应该能够添加类似的东西:

app/controllers/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'}

最新更新