2016-10-13 32 views
2

這是我的第一個堆棧溢出帖子,所以如果我提出的問題不好或者留下任何可以幫助回答問題的事情,我都會提前道歉。但請讓我知道,如果有任何其他信息會有所幫助,我會馬上回應!在Rails 5.0中使用Devise登錄/會話驗證錯誤消息

此外,我正在與一個名爲語義的前端CSS庫,所以我道歉你可能會在erb文件中看到任何clunkiness!

問題:我正在嘗試驗證用戶登錄過程,並且未顯示缺少電子郵件和密碼字段的錯誤。 (應用程序/視圖/設計/會話/ new.html.erb


什麼奇怪:我研究了很多其他的堆棧溢出文章和其他博客和成功顯示註冊過程驗證錯誤( 應用程序/視圖/設計/註冊/ new.html.erb


我所做的

  1. 需要在用戶模型中的驗證:

`

class User < ApplicationRecord 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
    has_many :courses 
    has_many :completions 
    has_many :htmls, through: :completions 

    validates :first_name, presence: true 
    validates :last_name, presence: true 
    validates :email, presence: true 
    validates :password, presence: true 
    validates :country, presence: true 
    validates :gender, presence: true 
    validates :goal, presence: true, length: { in: 10..100 } 
end 

`

  • 改性我的配置/區域設置/ en.yml文件包括以下內容:
  • '

    en: 
        activerecord: 
        errors: 
         models: 
         user: 
          attributes: 
          first_name: 
           blank: "Your First Name is Required" 
          last_name: 
           blank: "Your Last Name is Required" 
          email: 
           blank: "Please enter an email address" 
           taken: "That email is already taken" 
           invalid: "Please enter a valid email address" 
          password: 
           blank: "Please Specify a Password" 
           too_short: "Password must be at least %{count} characters long" 
          country: 
           blank: "Please Select Your Country of Residence" 
          gender: 
           blank: "Please Select Your Gender" 
          goal: 
           blank: "Please describe a goal you would like to achieve" 
           too_long: "Your goal must be shorter than %{count} characters long" 
           too_short: "Your goal must be longer than %{count} characters long" 
    

    '

  • 寫我應用程序/視圖/設計/會話/ new.html.erb文件
  • <h1><span class="hashtag">#</span> log in</h1> 
     
    
     
         <%= simple_form_for(resource, as: resource_name, url: session_path(resource_name), html: { class: "ui form" }) do |f| %> 
     
    
     
          <div class="field"> 
     
           <%= f.input :email, autofocus: true %> 
     
          </div> 
     
    
     
          <div class="field"> 
     
           <%= f.input :password %> 
     
          </div> 
     
    
     
           <div class="registration_buttons"> 
     
           <%= link_to 'register', new_user_registration_path, { class: "ui left floated huge register button" }%> 
     
           <%= link_to "forgot password", new_password_path(resource_name), class: "ui left floated huge register button" %><br /> 
     
           <%= f.button :submit, "log in", class: "ui right floated huge login button" %> 
     
           </div> 
     
    
     
         <% end %>

    1. 上面的設計/會話的查看頁面s/new不會顯示我在config/locales/en.yml中自定義的消息,但這裏是app/views/devise/registrations/new/html.erb這與以前的代碼片段非常相似並正確顯示消息。

     <h1><span class="hashtag">#</span> welcome to the community</h1> 
     
    
     
         <%= simple_form_for(resource, as: resource_name, url: user_registration_path, html: { class: 'ui registration form' }) do |f| %> 
     
    
     
         <div class="two fields"> 
     
          <div class="field"> 
     
          <%= f.input :first_name, autofocus: true %> 
     
          </div> 
     
          <div class="field"> 
     
          <%= f.input :last_name %> 
     
          </div> 
     
         </div> 
     
    
     
         <div class="two fields"> 
     
          <div class="field"> 
     
          <%= f.input :email%> 
     
          </div> 
     
          <div class="field"> 
     
          <%= f.input :password%> 
     
          </div> 
     
         </div> 
     
    
     
         <div class="two fields"> 
     
          <div class="field"> 
     
          <%= render "countries_dropdown" %> 
     
          <div class="error"><%= @user.errors[:country][0] %></div> 
     
          </div> 
     
          <div class="field"> 
     
          <%= render "gender_dropdown" %> 
     
           <div class="error"><%= @user.errors[:gender][0] %></div> 
     
          </div> 
     
         </div> 
     
    
     
         <div class="field"> 
     
          <%= f.input :goal, wrapper_html: { id: "goal_input" }, 
     
          label_html: { id: "varchar_count" }, 
     
          input_html: { id: "goal_input_field" } %> 
     
         </div> 
     
    
     
         <div class="registration_buttons"> 
     
          <%= f.button :submit, "register", class: "huge ui right floated register button" %> 
     
          <%= link_to "log in", new_session_path(resource), { class: "ui huge login button" } %> 
     
         </div> 
     
    
     
         <% end %>

  • 我還創建了自己的註冊控制器,其從設計繼承:: RegistrationsController:
  • '

    class RegistrationsController < Devise::RegistrationsController 
        #prepend_before_action :require_no_authentication, only: [:new, :create, :cancel] 
        #prepend_before_action :authenticate_scope!, only: [:edit, :update, :destroy] 
        #prepend_before_action :set_minimum_password_length, only: [:new, :edit] 
    
        # GET /resource/sign_up 
        def new 
        build_resource({}) 
        yield resource if block_given? 
        respond_with resource 
        end 
    
        # POST /resource 
        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 
        end 
        end 
    
        # GET /resource/edit 
        def edit 
        render :edit 
        end 
    
        # PUT /resource 
        # We need to use a copy of the resource because we don't want to change 
        # the current user in place. 
        def update 
        self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key) 
        prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email) 
    
        resource_updated = update_resource(resource, account_update_params) 
        yield resource if block_given? 
        if resource_updated 
         if is_flashing_format? 
         flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ? 
          :update_needs_confirmation : :updated 
         set_flash_message :notice, flash_key 
         end 
         bypass_sign_in resource, scope: resource_name 
         respond_with resource, location: after_update_path_for(resource) 
        else 
         clean_up_passwords resource 
         respond_with resource 
        end 
        end 
    
        # DELETE /resource 
        def destroy 
        resource.destroy 
        Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name) 
        set_flash_message! :notice, :destroyed 
        yield resource if block_given? 
        respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) } 
        end 
    
        # GET /resource/cancel 
        # Forces the session data which is usually expired after sign 
        # in to be expired now. This is useful if the user wants to 
        # cancel oauth signing in/up in the middle of the process, 
        # removing all OAuth session data. 
        def cancel 
        expire_data_after_sign_in! 
        redirect_to new_registration_path(resource_name) 
        end 
    
        protected 
    
        def update_needs_confirmation?(resource, previous) 
        resource.respond_to?(:pending_reconfirmation?) && 
         resource.pending_reconfirmation? && 
         previous != resource.unconfirmed_email 
        end 
    
        # By default we want to require a password checks on update. 
        # You can overwrite this method in your own RegistrationsController. 
        def update_resource(resource, params) 
        resource.update_with_password(params) 
        end 
    
        # Build a devise resource passing in the session. Useful to move 
        # temporary session data to the newly created user. 
        def build_resource(hash=nil) 
        self.resource = resource_class.new_with_session(hash || {}, session) 
        end 
    
        # Signs in a user on sign up. You can overwrite this method in your own 
        # RegistrationsController. 
        def sign_up(resource_name, resource) 
        sign_in(resource_name, resource) 
        end 
    
        # The path used after sign up. You need to overwrite this method 
        # in your own RegistrationsController. 
        def after_sign_up_path_for(resource) 
        after_sign_in_path_for(resource) 
        end 
    
        # The path used after sign up for inactive accounts. You need to overwrite 
        # this method in your own RegistrationsController. 
        def after_inactive_sign_up_path_for(resource) 
        scope = Devise::Mapping.find_scope!(resource) 
        router_name = Devise.mappings[scope].router_name 
        context = router_name ? send(router_name) : self 
        context.respond_to?(:root_path) ? context.root_path : "/" 
        end 
    
        # The default url to be used after updating a resource. You need to overwrite 
        # this method in your own RegistrationsController. 
        def after_update_path_for(resource) 
        signed_in_root_path(resource) 
        end 
    
        # Authenticates the current scope and gets the current resource from the session. 
        def authenticate_scope! 
        send(:"authenticate_#{resource_name}!", force: true) 
        self.resource = send(:"current_#{resource_name}") 
        end 
    
        def sign_up_params 
        params.require(:user).permit(:first_name, :last_name, :email, :password, :country, :gender, :goal) 
        end 
    
        def account_update_params 
        params.require(:user).permit(:first_name, :last_name, :email, :current_password, :password, :country, :gender, :goal) 
        end 
    
        def translation_scope 
        'devise.registrations' 
        end 
        #def update_resource(resource, params) 
        #resource.update_without_password(params) 
        #end 
    end 
    

    '

  • 結果,我已經改變了路由文件以包括:
  • '

    Rails.application.routes.draw do 
        devise_for :users, :controllers => { registrations: 'registrations' } 
    
        devise_scope :user do 
        authenticated :user do 
         root 'users#index', as: :authenticated_root 
        end 
        unauthenticated do 
         root 'devise/sessions#new', as: :unauthenticated_root 
        end 
    end 
    

    '

  • 這裏是我的耙路線: My Rake Routes

  • 這裏是我的app/views/layouts/application.html.erb根據要求!

  • '

    1 <html> 
        2 <head> 
    ~ 3  <title><%= content_for?(:title) ? yield(:title) : 'KodoTechnologies' %></title> 
        4  <%= csrf_meta_tags %> 
        5 
        6  <%= stylesheet_link_tag "application", params[:controller], :media => "all" %> 
        7  <%= javascript_include_tag "application", params[:controller] %> 
        8 </head> 
        9 
        10 <body> 
    ~ 11 
    + 12  <% if current_user && current_user.admin? %> 
    + 13  <div id="admin_menubar"> 
    + 14   <%= render "customs/admin_menubar" %> 
    + 15  </div> 
    + 16  <% end %> 
    + 17 
    + 18  <% if current_user && !current_user.admin? %> 
    + 19  <div id="student_menubar"> 
    + 20   <%= render "customs/student_menubar" %> 
    + 21  </div> 
    + 22  <% end %> 
    + 23 
    + 24  <div id="content"> 
    + 25  <%= content_for?(:content) ? yield(:content) : yield %> 
    + 26  </div> 
    + 27 
        28 </body> 
    + 29 
    + 30 <%= content_for(:page_scripts) %> 
    + 31 
        32 </html> 
    

    '


    摘要

    1. 前面說過,這對記數的工作原理相同的錯誤顯示方法配給過程不適用於登錄過程

    2. 我覺得我的限制來自我對Devise SessionsController如何工作的有限理解。我創建了自己的RegistrationsController,它繼承了Devise :: RegistrationsController並且成功了,但我不知道如何使用SessionsController,因此無法調試甚至實現我自己的會話驗證過程#使用flash錯誤消息創建


    問:

    誰能告訴我,我可能是做錯了多數民衆贊成允許被顯示在註冊驗證消息,但沒有在日誌中的形式?

    讓我知道如果我遺漏了任何東西,我很樂意提供您需要的一切。先謝謝你!

    +0

    你能添加你的'app/views/layouts/application.html.erb'文件呢? Devise的幫助建議使用它來顯示Flash消息 - 我想知道是否有問題。 – ArtOfCode

    +0

    你好ArtOfCode!我剛剛去了,並將它添加爲列表中的第8項。我不確定這會有多大的幫助,因爲它會產生其他佈局(嵌套佈局),並且還有一大堆部分,並從那裏產生。 事情是,我會完全使用Flash消息,如果我知道驗證實際發生的位置:( 我不認爲我瞭解設計SessionsController足以知道我需要爲自定義驗證編寫適當的代碼和閃存郵件存儲:o感謝您的關注! –

    回答

    0

    Devise幫助建議您在應用程序佈局文件中包含Flash消息。你的似乎沒有這些;嘗試包括他們。這將在整個應用程序中啓用閃光消息,而不管正在播放什麼路由。

    對於基本的Rails項目,你可能會使用這樣的:如果你使用的引導與Glyphicons

    <% if flash[:notice].present? %> 
        <p><%= flash[:notice] %></p> 
    <% end %> 
    
    <% if flash[:alert].present? %> 
        <p><%= flash[:alert] %></p> 
    <% end %> 
    

    ,我發現下面的模板作品相當不錯:

    <% if flash[:notice].present? %> 
        <div class="alert alert-info"> 
        <span class="glyphicon glyphicon-info-sign"></span> 
        <%= flash[:notice] %> 
        </div> 
    <% end %> 
    
    <% if flash[:alert].present? %> 
        <div class="alert alert-danger"> 
        <span class="glyphicon glyphicon-exclamation-sign"></span> 
        <%= flash[:alert] %> 
        </div> 
    <% end %> 
    
    +0

    您好ArtOfCode,OP在這裏,感謝您的反饋! –

    +0

    我還有其他一些問題出現,但我認爲你的回答是我開始的好地方,我他們中的大多數都出來了,非常感謝! –

    +0

    如果你或者其他任何可能閱讀這篇文章的人可以詳細說明我可以如何定製這些消息,或者最終如何訪問用戶名/密碼與i進行比較的代碼n設計,所以我可以創建包含非常特定的Flash消息的自定義Flash會話變量。我認爲這可能有助於顯示獨特的內聯驗證錯誤消息以及其他一些情況。如果有人能幫助我理解上面突出顯示的過程,我很樂意接受這個答案,再次感謝ArtOfCode! –