Reputation: 687
Context - I am creating a simple authentication, following the line by line instruction from https://www.youtube.com/watch?v=Hw6WtWJwRtU.
Steps completed so far -
Error - On entering the user name and passowrd in the login form, I am getting error No method defined.
Steps taken so far to correct - 1. Checked the table schema, the table has column password_digest and column type is varchar 2. gem list - mentions that bcrypt is installed. I closed rails server and restarted again too.
Please find the snippet -
User Model -
class User < ActiveRecord::
attr_accessible :user_name, :password
has_secure_password
belongs_to :UserType
def find_by_name(name)
user = User.find_by(user_name: name)
end
end
Sessions Controller -
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_name(params[:user_name])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "Logged In"
else
flash.now.alert = "Email or password is invalid"
render new
end
end
end
Session View -
<h1>Log In</h1>
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :user_name %>
<%= text_field_tag :user_name, params[:user_name] %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<div class="action"><%= submit_tag "Log In" %></div>
<%end%>
users table -
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_name" varchar, "password_digest" varchar, "UserType_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
CREATE INDEX "index_users_on_UserType_id" ON "users" ("UserType_id");
Routes -
Rails.application.routes.draw do
resources :sessions
resources :feedback_details
resources :users
resources :meetings
resources :user_types
root 'sessions#new'
Error Log -
Started POST "/sessions" for 127.0.0.1 at 2017-01-29 14:24:43 +0530
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BvROFNObaYS/8Fq8wjTLwh6DiWo5rcLNnfvpdhlxgD2vhEmx2ErXWJRA8ku6bHYBRZ8W2imI2N/+SMMlnuWz+w==", "user_name"=>"tushar", "password"=>"[FILTERED]", "commit"=>"Log In"}
Completed 500 Internal Server Error in 24ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `has_secure_password' for ActiveRecord:Module):
app/models/user.rb:2:in `<top (required)>'
app/controllers/sessions_controller.rb:6:in `create'
Rendered /usr/lib/ruby/vendor_ruby/action_dispatch/middleware/templates/rescues/_source.erb (8.6ms)
Rendered /usr/lib/ruby/vendor_ruby/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.0ms)
Rendered /usr/lib/ruby/vendor_ruby/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.8ms)
Rendered /usr/lib/ruby/vendor_ruby/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (31.6ms)
Rendered /var/lib/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.7ms)
Rendered /var/lib/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.6ms)
Rendered /var/lib/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.6ms)
Rendered /var/lib/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.6ms)
Rendered /var/lib/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (25.3ms)
Rendered /var/lib/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.6ms)
Rendered /var/lib/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.8ms)
Rendered /var/lib/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (51.1ms)
Please let me know, if I need to provide more information.
Upvotes: 0
Views: 359
Reputation: 1636
You're missing a teeny bit of code here.
class User < ActiveRecord::Base
Upvotes: 0