-2

I am new in ruby. I have tried to make a login page. But I am getting this error.

enter image description here

Here is my coding -

users_controller.rb

def login
    @title = 'Login'
    render layout: 'login'
  end
  def create_login
    #user = User.find_by(username: params[:user][:username].downcase).first
    user = User.where("username = ?", params[:user][:username].downcase).first
    if user && user.authenticate(params[:user][:password])
      # Log the user in and redirect to the user's show page.
      log_in @user
      redirect_to @user
    else
      flash[:danger] = 'Invalid email/password combination' # Not quite right!
      render 'new'
    end
  end

router.rb

get    'login'   => 'users#login'
post   'login'   => 'users#create_login'

sessions_helper.rb

def log_in(user)
    session[:user_id] = user.id
  end

user.rb

class User < ActiveRecord::Base
  validates :name, presence: true


  validates :name, length: { minumum:2, maximum: 30 }


  #validates :password, presence: true
  #validates :password, length: { in: 6..20 }
  validates :password, :presence =>true,
                    :length => { :minimum => 6, :maximum => 40 },
                    :confirmation =>true
  #validates_presence_of :password_confirmation, if: :password_changed?


  #validates_uniqueness_of :username
  #validates :email, confirmation: true
  validates :username, :presence => true,
           :uniqueness => { :case_sensitive => false }

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, :presence => true,
           :format => { :with => email_regex },
           :uniqueness => { :case_sensitive => false }

  validates :dob, presence: true
  #dob_regex = /\d{2}\-\d{2}\-\d{4}/       
  #validates :dob, :presence => true, :format => { :with => dob_regex }
end
Chinmay235
  • 3,236
  • 8
  • 62
  • 93
  • Please post your `user.rb` model. – Pavan Apr 12 '16 at 08:22
  • @Pavan I am updating my question. Please check `user.rb` model – Chinmay235 Apr 12 '16 at 08:23
  • Well, you should be having `authenticate` method in `user.rb` model. Are you following any tutorial to make this authenticate logic in your app? – Pavan Apr 12 '16 at 08:26
  • It looks like you should be including some authentication gem perhaps. – Max Williams Apr 12 '16 at 08:28
  • Yes I am following https://www.railstutorial.org/book/log_in_log_out tutorial. But there are no any authentication. Please check. And let me know how to give authentication in my page. – Chinmay235 Apr 12 '16 at 08:28
  • 1
    You should create your authenticate method. http://railscasts.com/episodes/250-authentication-from-scratch –  Apr 12 '16 at 08:42

2 Answers2

1

The tutorial that you're following uses bcrypt gem. Check if it is on your gemfile and bundle install.

For authenticate method follow this tutorial from railscasts using bcrypt

Part of railscast authentication method:

def self.authenticate(email, password)
  user = find_by_email(email)
  if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
    user
  else
    nil
  end
end
HashRocket
  • 778
  • 1
  • 5
  • 15
0

In your user model you need to add has_secure_password

and

Add bcrypt (~> 3.1.7) to Gemfile to use has_secure_password:

gem 'bcrypt', '~> 3.1.7'

and

class User < ActiveRecord::Base
  #your other code
  has_secure_password
end

now access the authenticate method

More on it

on your mention link of the book you refer you can see you missed it.

Go to chapter 6

Listing 6.39: The complete implementation for secure passwords. green app/models/user.rb**

Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • I am following your code. But i am getting this error message `cannot load such file -- bcrypt_ext` – Chinmay235 Apr 12 '16 at 09:37
  • https://github.com/codahale/bcrypt-ruby/issues/116 this is actual solution..specifically you can refer http://stackoverflow.com/questions/33588735/loaderror-cannot-load-such-file-bcrypt-ext-on-windows-2008-x64-server – Rajarshi Das Apr 12 '16 at 09:46