试图让Devise在错误的注册时生成flash错误消息。例如“电子邮件不能为空”或“密码太短”。
在查看railscast第210集时,这似乎是Devise的开箱即用功能(通过validatable),但在我的实例中,没有为注册生成Flash消息。注意flash消息确实在此应用程序的其他实例中生成,例如发送重置指令,删除帐户等...(如果您想亲眼看看,可以在生产网站ninjaspeak.com上看到行为)
有关为何会这样做的任何想法?
使用Devise 2.1.2和Rails 3.2.13
devise.en.yml:
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
en:
errors:
messages:
expired: "has expired, please request a new one"
not_found: "not found"
already_confirmed: "was already confirmed, please try signing in"
not_locked: "was not locked"
not_saved:
one: "1 error prohibited this %{resource} from being saved:"
other: "%{count} errors prohibited this %{resource} from being saved:"
devise:
failure:
already_authenticated: 'You are already signed in.'
unauthenticated: 'You need to sign in or sign up before continuing.'
unconfirmed: 'You have to confirm your account before continuing.'
locked: 'Your account is locked.'
invalid: 'Invalid email or password.'
invalid_token: 'Invalid authentication token.'
timeout: 'Your session expired, please sign in again to continue.'
inactive: 'Your account was not activated yet.'
sessions:
signed_in: ''
signed_out: ''
passwords:
send_instructions: 'You will receive an email with instructions about how to reset your password in a few minutes.'
updated: 'Your password was changed successfully. You are now signed in.'
updated_not_active: 'Your password was changed successfully.'
send_paranoid_instructions: "If your e-mail exists on our database, you will receive a password recovery link on your e-mail"
confirmations:
send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.'
send_paranoid_instructions: 'If your e-mail exists on our database, you will receive an email with instructions about how to confirm your account in a few minutes.'
confirmed: 'Your NinjaSpeak account was successfully confirmed. You are now signed in.'
registrations:
signed_up: 'Welcome! You have signed up successfully.'
signed_up_but_unconfirmed: 'A message with a confirmation link has been sent to your email address. Please open the link to activate your NinjaSpeak account.'
signed_up_but_inactive: 'You have signed up successfully. However, we could not sign you in because your account is not yet activated.'
signed_up_but_locked: 'You have signed up successfully. However, we could not sign you in because your account is locked.'
updated: 'You updated your account successfully.'
update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address."
destroyed: 'Your NinjaSpeak account was successfully cancelled.'
unlocks:
send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.'
unlocked: 'Your account has been unlocked successfully. Please sign in to continue.'
send_paranoid_instructions: 'If your account exists, you will receive an email with instructions about how to unlock it in a few minutes.'
omniauth_callbacks:
success: 'Successfully authorized from %{kind} account.'
failure: 'Could not authorize you from %{kind} because "%{reason}".'
mailer:
confirmation_instructions:
subject: 'Confirmation instructions'
reset_password_instructions:
subject: 'Reset password instructions'
unlock_instructions:
subject: 'Unlock Instructions'
application.html.erb:
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, id: "flash_#{name}" %>
<% end %>
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :natives_language, :next_language, :remember_me, :bookmark
# attr_accessible :title, :body
validates_presence_of :natives_language, :next_language
end
编辑以显示css:
#flash_alert {
padding-top: 16px;
float: right;
}
#flash_notice {
padding-top: 16px;
float: right;
}
答案 0 :(得分:2)
据我所知,设计没有管理验证的错误,这就是为什么你不能这样做,而且在我看来,我认为这不是一个好的行为。所以我建议你使用正常显示验证错误。
如果您想改进验证消息以在客户端验证它们,而无需到达 服务器,您可以使用“jquery-validation-rails”并添加到application.js或其他类似的javascript文件:
//validador jquery
$('#new_user').validate({
rules: {
'user[password]': {
required: true,
maxlength: 100
},
'user[email]': {
required: true,
email: true,
maxlength: 150
}
},
messages: {
'user[password]': {
required: "Required Field",
maxlength: "Name too big, max size 100"
},
'user[email]': {
required: "Required Field",
email: "Invalid email",
maxlength: "Email too big, max size 150"
}
}
});
如果您想更改错误消息的颜色,可以使用css
来完成/* Change color to error messages for validation with jQuery */
label.error{
color: white !important;
font-weight: normal !important;
}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :password,
presence: true,
length: { maximum: 100 }
validates :email,
presence: true,
length: { maximum: 150 },
format: { with: VALID_EMAIL_REGEX }
我喜欢用这种方式来捕捉rails中的flash消息,它右边有一个“X”按钮,文本“close”以使其更具用户体验。
<!-- Flash Notice for Rails notifications -->
<% if notice %>
<p class="alert alert-success">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only" style="font-size: 14px !important;"> Close</span></button>
<%= notice %>
</p>
<% end %>
<% if alert %>
<p class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only" style="font-size: 14px !important;"> Close</span></button>
<%= alert %>
</p>
<% end %>
此外,您可以在Rails 3或4.1中使用名为“growlyflash”的gem,它可以使消息样式增长,从而使消息看起来很酷。如果您使用growlyflash,则不需要flash消息的代码,而是在“application.html.erb”中使用它:
<%= growlyflash_static_notices %>
如果你想在点击时让消息消失,只需要在一个名为“growlyflash.js”的文件中添加这个javascript(只是为了保持代码的组织)。另外,我添加了一种解决危险消息颜色问题的方法。
// The message will disappear when get clicked
jQuery(function() {
$(document).on('click.alert.data-api', '[data-dismiss="alert"]', function(e) {
return e.stopPropagation();
});
return $(document).on('touchstart click', ".bootstrap-growl", function(e) {
e.stopPropagation();
$('[data-dismiss="alert"]', this).click();
return false;
});
});
// This part of code is VERY IMPORTANT, because it solved an issue for rendering color
// to the danger message.
$.bootstrapGrowl.defaults = $.extend(true, {}, $.bootstrapGrowl.defaults, {
type_mapping: {
alert: 'warning',
error: 'danger',
notice: 'info',
success: 'success'
}
});
希望它有所帮助:D
答案 1 :(得分:0)
好吧,它可能就像你没有将任何CSS样式应用到你的警报容器一样简单(如果你认为你应该看到一个flash消息,你可能会在那里看到你的DOM)。
以下是我根据我想要输出的消息类型使用我的flash消息
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %>">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>
答案 2 :(得分:0)
问题出在我的views / devise / registrations / new.html.erb文件中。
下边
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
我不得不添加设计错误消息helper:
<%= devise_error_messages! %>
现在,Flash邮件会出现在无效注册状态,例如“电子邮件不能为空”,“密码与确认不符”等...