1

I am getting a strange error with Devise. When I load the homepage it doesn't like this code:

<% if signed_in? %><p>Welcome back, 
   <%= current_user.first_name %> <%= current_user.last_name %></p><% end %>

Because it says:

undefined method `encrypt' for #<Class:0x3720300>

I did a search for "encrypt" in my app and all it returned was my sessions_helper.rb:

module SessionsHelper
  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def sign_out
    current_user.update_attribute(:remember_token, User.encrypt(User.new_remember_token))
    cookies.delete(:remember_token)
    self.current_user = nil
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end
end

What does this mean? Do I need to reinstall something?

2
  • Do you have encrypt method in User model? As your signed_in? method calls current_user method where you're using User.encrypt that seems to be the problematic part. Commented Apr 2, 2014 at 19:32
  • I don't have an encrypt method in my User model. Does Devise generate this sessions helper? How do I fix this? Commented Apr 2, 2014 at 19:45

1 Answer 1

3

If I am not wrong, you are following Michael Hartl's Rails Tutorial as your code resembles to it. If thats true then, you need to add a class method encrypt in User model

class User < ActiveRecord::Base
  ...
  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end
  ...
end
Sign up to request clarification or add additional context in comments.

4 Comments

I started using his tutorial, but then went with Devise instead. I guess some of my files are left over from doing his tutorial. Not sure how to repair this, might need to just start over clean!
I knew it that there was some mix-up with Michael Hartl's tutorial and Devise. Well that resolves your question why you were getting error about missing encrypt method. What I would recommend is to totally start from scratch using Devise else it will get all mixed up. I would appreciate if you accept the answer.
I would say remove this helper as you don't need it with devise. Devise incorporate with such methods. You can directly use current_user. Check this link devise.plataformatec.com.br/#getting-started/…
YES! Deleting that file worked! (For now, I'll continue to test). Thank you, both! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.