gpt4 book ai didi

ruby-on-rails - Rails/Device 注册不正确时不会生成 Flash 消息

转载 作者:数据小太阳 更新时间:2023-10-29 07:35:54 25 4
gpt4 key购买 nike

尝试使 Devise 在不正确的注册时生成 flash 错误消息。例如“电子邮件不能为空”或“密码太短”。

在查看 railscast episode 210 时,这似乎是 Devise 开箱即用的功能(通过可验证),但在我的实例中,没有生成用于注册的闪存消息。请注意,此应用程序的其他实例中会生成闪现消息,例如发送重置指令、删除帐户等...(如果您想亲自查看,可以在生产网站 ninjaspeak.com 上查看行为)

对为什么会这样有任何想法吗?

将 Devise 2.1.2 与 Rails 3.2.13 结合使用

设计.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'

应用程序.html.erb:

<% flash.each do |name, msg| %>
<%= content_tag :div, msg, id: "flash_#{name}" %>
<% end %>

用户.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;
}

最佳答案

回答问题


据我所知,devise 不会管理验证错误,这就是为什么你不能这样做,而且以我的拙见,我认为这不是一个好的行为。所以我建议您使用正常显示来显示验证错误。

如果您想改进验证消息以在客户端验证它们,而无需到达服务器,您可以使用“jquery-validation-rails”并添加到 application.js 或其他 javascript 文件,如下所示:

验证_jquery.js

//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来做

validation_jquery.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 消息

我喜欢用这种方式在 rails 中捕获 flash 消息,它在右侧有一个“X”按钮和“关闭”文本,使其更具用户体验。

<!-- Flash Notice for Rails notifications -->
<% if notice %>
<p class="alert alert-success">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</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">&times;</span><span class="sr-only" style="font-size: 14px !important;"> Close</span></button>
<%= alert %>
</p>
<% end %>

超酷的 Growlyflash 消息

此外,您还可以在 Rails 3 或 4.1 中使用名为“growlyflash”的 gem,它可以使用粗暴的消息样式来闪烁消息,并使其看起来很酷。如果你使用 growlyflash,你不需要 flash 消息的代码,而是你将在你的“application.html.erb”中使用它:

<%= growlyflash_static_notices %>

如果你想让消息在点击时消失,只需要在一个名为“growlyflash.js”的文件中添加这个javascript(只是为了保持代码的组织性)。我还添加了一种方法来解决危险消息颜色的问题。

Growlyflash.js

// 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

关于ruby-on-rails - Rails/Device 注册不正确时不会生成 Flash 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27582232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com