【问题标题】:devise from redirection routing issue从重定向路由问题设计
【发布时间】:2017-06-24 05:06:48
【问题描述】:

我的路线使用 devise 时遇到问题。

我修改了我的devise/sessions/new 视图,使其同时具有登录和注册表单。

一切正常,如果所有字段都正确填写,我可以在同一视图 (sign_in) 中使用任一表单登录或注册。但是,如果注册表中有错误(缺少电子邮件,或错误的确认密码,用户将被重定向到具有http://0.0.0.0:3000/users url 的“经典”设计sign_up 页面,而不是停留在users/sign_in 页面和在那里显示错误。

我的路线如下:

devise_for :users, controllers: { sessions: "users/sessions"}
as :user do
  get 'sign_in', :to => 'devise/sessions#new'
end

我的注册表有以下声明:

 <%= form_for(resource, as: resource_name,  class: "registrtion-form", url: registration_path(resource_name)) do |f| %>

我的应用程序助手设置用户资源:

 def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-5


    【解决方案1】:

    我认为您可以尝试覆盖 RegistrationsController.create 方法来做到这一点。

    class User::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
          # respond_with resource
          # custom code to redirect to your sign up form if have an error in the registration form.
          redirect_to ...
        end
      end
    end
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      相关资源
      最近更新 更多