![](/img/trans.png)
[英]Rails 4, Devise - Error messages on Login aren't working properply
[英]Working with Devise Login / Session Validation Error Messages in Rails 5.0
这是我的第一个堆栈溢出帖子,因此,如果我提出的问题不佳或遗漏任何可以帮助回答问题的内容,我会向您道歉。 但是,请让我知道是否还有其他信息可以帮助您,我会立即回复!
另外,我正在使用一个名为Semantic的前端CSS库,因此对于erb文件中可能出现的任何笨拙之处,我深表歉意!
问题 :我正在尝试验证用户登录过程,并且由于缺少电子邮件和密码字段而未显示错误。 ( app / views / devise / sessions / new.html.erb )
奇怪的是 :我研究了许多其他堆栈溢出文章和其他博客,并成功显示了注册过程的验证错误( app / views / devise / registrations / new.html.erb )
我所做的 :
`
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:
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"
“
<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 %>
<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 %>
“
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
“
这是我的耙路: 我的耙路
根据要求,这是我的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>
“
总结 :
如前所述,适用于注册过程的相同错误显示方法不适用于登录过程
我觉得我的局限性来自于对Devise SessionsController如何在后台工作的有限了解。 我已经创建了自己的从Devise :: RegistrationsController继承的RegistrationsController,并取得了一定程度的成功,但是我不知道如何使用SessionsController,因此无法调试甚至无法对带有闪存错误消息的session#create实施我自己的验证过程
题:
谁能告诉我我可能在做错什么,使验证消息显示在注册中,但不在登录表单中显示?
让我知道是否遗漏了任何东西,我很乐意提供你们需要的一切。 先感谢您!
Devise帮助建议您将Flash消息包含在应用程序布局文件中。 您的似乎没有这些; 尝试包括它们。 这将在整个应用程序中启用Flash消息,而不管命中了什么路由。
对于基本的Rails项目,您可以使用以下代码:
<% if flash[:notice].present? %>
<p><%= flash[:notice] %></p>
<% end %>
<% if flash[:alert].present? %>
<p><%= flash[:alert] %></p>
<% end %>
如果您将引导程序与Glyphicons一起使用,我发现以下模板效果很好:
<% 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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.