gpt4 book ai didi

ruby-on-rails - Rails - 设计 - 登录时出现错误消息?

转载 作者:行者123 更新时间:2023-12-03 07:52:45 24 4
gpt4 key购买 nike

我该如何制作 f.error_messages 在这里工作,还是应该使用闪光灯?
如果是这样,应该在 session_controller 中覆盖什么?

<h2>Create an account</h2>    
<% form_for resource_name, resource, :url => registration_path(resource_name) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :email %><br />
<%= f.text_field :email, :class => :big %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password, :class => :big %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, :class => :big %>
</p>

<p><%= f.submit "Create", :class => :submit %></p>
<% end %>

附注。 f.error_messages 用于创建帐户完全正常。

最佳答案

不可否认,有点hacky,但我正在使用这个助手(app/helpers/devise_helper.rb)来抓取闪光灯并使用那些如果设置然后默认为resource.errors .

module DeviseHelper

def devise_error_messages!
flash_alerts = []
error_key = 'errors.messages.not_saved'

if !flash.empty?
flash_alerts.push(flash[:error]) if flash[:error]
flash_alerts.push(flash[:alert]) if flash[:alert]
flash_alerts.push(flash[:notice]) if flash[:notice]
error_key = 'devise.failure.invalid'
end

return "" if resource.errors.empty? && flash_alerts.empty?
errors = resource.errors.empty? ? flash_alerts : resource.errors.full_messages

messages = errors.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t(error_key, :count => errors.count,
:resource => resource.class.model_name.human.downcase)

html = <<-HTML
<div id="error_explanation">
<h2>#{sentence}</h2>
<ul>#{messages}</ul>
</div>
HTML

html.html_safe
end

end

关于ruby-on-rails - Rails - 设计 - 登录时出现错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4635986/

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