I am just installing Devise 4.1 into my Rails 5.0.0.rc1 project.
In my application.html.erb
, I have this error message helper:
<%= devise_error_messages! %>
I have overridden it with this boilerplate helper:
module DeviseHelper
def devise_error_messages!
return "" unless devise_error_messages?
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t("errors.messages.not_saved",
:count => resource.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
def devise_error_messages?
!resource.errors.empty?
end
end
Yet when I reload my site and go to my root_page
, I get this error:
NameError at /
undefined local variable or method `resource' for #<#<Class:0x007f8105dc24f8>:0x007f8105db73f0>
The error happens at this line in my app/helpers/devise_helper.rb
def devise_error_messages?
!resource.errors.empty?
end
In my controllers I have added:
before_action :authenticate_user!, except: [:index, :show]
What could be causing this?