Rails - undefined `authenticate 'method

When I try to authenticate a user, I get the following error.

NoMethodError (undefined method authenticate 'for #) `

user.authenticate ("password") succeeds (that is, returns a user object) when I execute a command from the rails console.

irb(main):008:0> user.authenticate("sanket")
=> #<User id: 2, name: "Sanket", email: "sanket.meghani@infibeam.net", created_at: "2014-02-14 08:58:28", updated_at: "2014-02-14 08:58:28", password_digest: "$2a$10$KNuBgvqVDIErf3a24lMcaeNt1Hyg0I8oreIQSEYsXY4T...", remember_token: "3b64273a4fcced7c8bf91f7f7490e60a5919658d">

However, when I put user.authenticate in the helper class and access it using the url, it says

undefined method 'authenticate' for #<ActiveRecord::Relation:0x007fd9506dce38>

I follow the Rails Tutorial

My user model is as follows:

class User < ActiveRecord::Base        
    attr_accessible :email, :name, :password, :password_confirmation
    has_secure_password

    .
    .
    .
end

The related method in session_helper.rb looks like this:

def create
    user = User.where(:email => params[:session][:email].downcase)

    if user && user.authenticate(params[:session][:password])
        sign_in user
        redirect_to user
    else
        flash.now[:error] = 'Invalid email/password combination'
        render "new"
    end
end

It throws an error above in the line if user && user.authenticate(params[:session][:password]).

What surprises me is that it works with the rails console, however the same call does not work with the rails server.

+3
source share
1 answer

, where , .

: ? ( Rails)

first , :

user = User.where(:email => params[:session][:email].downcase).first
+11

All Articles