我在我的应用程序上使用设计,并且登录失败时我没有收到错误消息。设计不显示登录错误消息
我在登录页面上闪烁[:notice]和flash [:alert]。
我尝试了很多东西,当我从应用程序控制器中删除'protect_from_forgery'时,我收到错误消息。
另外我在我的应用程序上使用Cancan,它可以是任何问题吗?有任何想法吗?
谢谢
我在我的应用程序上使用设计,并且登录失败时我没有收到错误消息。设计不显示登录错误消息
我在登录页面上闪烁[:notice]和flash [:alert]。
我尝试了很多东西,当我从应用程序控制器中删除'protect_from_forgery'时,我收到错误消息。
另外我在我的应用程序上使用Cancan,它可以是任何问题吗?有任何想法吗?
谢谢
我猜测真实性验证失败。你的表格是否发送了带有帖子的authenticity_token?如果删除protect_from_forgery
修复它,这几乎肯定是问题所在。
确保所有未得到的请求都发送一个authenticity_token
参数,其中的值由rails函数form_authenticity_token
返回。如果您在视图中使用form_for
,则这应该是自动发生的。检查你的html以确保表格中的真实性标记应与返回form_authenticity_token
方法的值相匹配。
不可否认,有点不好意思,但我使用这个帮助器(app/helpers/devise_helper.rb)来抓取闪烁并使用这些设置,然后默认为resource.errors
。 Devise的会话控制器似乎没有使用模型错误,而是使用了Flash警报。这只是基于devise lib中的助手。
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
好答案@drewbob。我昨天就遇到了这个问题! – thefugal 2011-05-09 19:02:40