0

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?

marcamillion
  • 32,933
  • 55
  • 189
  • 380

1 Answers1

0

When setting up Device with 5.0.0.rc1 no version would work for me other than

gem "devise", '~> 4.0.0.rc1'

try changing the gem and bundling again

Edit and Final solution

Helper methods usually only get called inside the controller so by going to your root i guess you go to another controller. add this to your application helper

def resource_name
 :user
end

def resource
 @resource ||= User.new
end

def devise_mapping
 @devise_mapping ||= Devise.mappings[:user]
end
Artem Ankudovich
  • 458
  • 2
  • 14
  • Good idea, but I am still getting the same error. The version this installed was `Installing devise 4.0.3 (was 4.1.0)`. Wouldn't a later version work also? Either way, changing the gem doesn't fix this error. – marcamillion May 26 '16 at 21:31
  • I checked my bundle and yes 4.0.3 is what is working for me however i fount this it looks like a very similar issue [here](http://stackoverflow.com/questions/22232561/attribute-name-showing-on-devise-error) – Artem Ankudovich May 26 '16 at 22:20
  • I tried that solution but it didn't work for me. The issue is on the `resource`, not the rest of the daisy-chained method. – marcamillion May 26 '16 at 22:30
  • this will probably be my last guess but i found a explanation that the helper methods usually only get called inside the controller so by going to your root i guess you go to another controller Try putting in this code into your application_helper – Artem Ankudovich May 27 '16 at 16:01
  • Yes, it seems that has gotten rid of the error. Thanks much! – marcamillion May 27 '16 at 22:02
  • No pproblem glad i could help. – Artem Ankudovich May 28 '16 at 06:19