自定义设计路线以进行登录和注册

时间:2016-02-05 16:41:20

标签: ruby-on-rails-4 devise routes controllers

我在同一页面上有注册和登录表单,并希望在此页面上显示任何注册和登录错误,例如“电子邮件已存在”。我使用this solution自定义了注册控制器,并创建了custom DeviseFailure class,因此登录时出现的错误会重定向回到此页面。

我有两个问题:

  1. 如果由于表单中的错误而导致注册失败(例如,输入已注册的电子邮件地址),然后我尝试登录,我就会

    内部服务器错误 - inGET,接受的HTTP方法是OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK,VERSION-CONTROL,REPORT,CHECKOUT ,CHECKIN,UNCHECKOUT,MKWORKSPACE,UPDATE,LABEL,MERGE,BASELINE-CONTROL,MKACTIVITY,ORDERPATCH,ACL,SEARCH,MKCALENDAR和PATCH。

  2. 如果我然后重新加载重定向正确发生。我不知道如何解决这个错误。

    1. 如何仅针对登录表单或仅针对注册表单捕获错误?目前错误显示两次 - 每个表单一次。
    2. 的routes.rb

        authenticated :user do
          root 'preorders#by_campaign', as: :authenticated_root
        end
      
        root to: 'users#index'
      
        devise_for :users, :controllers => { :sessions => "sessions",
                                         :passwords => "passwords",
                                         :registrations => "registrations" }
      

      application_controller.rb

      def after_sign_in_path_for(resource)
        preorders_by_campaign_url
      end
      
      def after_sign_out_path_for(resource)
        root_url
      end
      

      registrations_controller.rb

      class RegistrationsController < Devise::RegistrationsController
        protected
      
        def after_sign_up_path_for(resource)
          after_sign_in_path_for(resource)
        end
      
        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
      end
      

      index.rb

      .user_form
        %h2 Sign up
      
        = form_for(resource, :as => resource_name, :url =>  registration_path(resource_name)) do |f| 
        = render partial: "errors", locals: {flash: flash}
      
          %div
            = f.label :email 
            = f.email_field :email
      
          %div
            = f.label :password 
            = f.password_field :password
      
          %div
            = f.label :password_confirmation 
            = f.password_field :password_confirmation
      
          %div= f.submit "Sign up"
      
       .user_form
         %p Already signed up? Sign in
      
         = form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| 
           = render partial: "errors", locals: {flash: flash}
           %div
            = f.label :email 
            = f.email_field :email
      
           %div
             = f.label :password 
             = f.password_field :password
      
           %div= f.submit "Sign in"
      

      _errors.html.haml

      -if flash.any?
        %ul{class: "errors"}
        - flash.each do |msg|
          %li=msg.first
      

0 个答案:

没有答案
相关问题