Ruby On Rails - 未定义的方法`has_secure_password'对于ActiveRecord:模块

时间:2017-01-29 12:30:46

标签: ruby-on-rails sqlite

上下文 - 我正在创建一个简单的身份验证,遵循https://www.youtube.com/watch?v=Hw6WtWJwRtU的逐行指令。

目前已完成的步骤 -

  1. 创建了一个用户表。该表包含user_name和password_digest
  2. 在提及的用户模型中' has_secure_password'
  3. 在gemfile中取消注释bcrypt并执行bundle install
  4. 创建了一个控制器会话,创建了一个新方法' Create'有形式来获取用户名和密码。
  5. 错误 - 在登录表单中输入用户名和passowrd时,我收到错误没有定义方法。

    到目前为止采取的纠正措施 - 1.检查表模式,表中有列password_digest,列类型为varchar 2. gem list - 提到安装了bcrypt。我关闭了rails服务器并重新启动了。

    请找到代码段 -

    用户模型 -

    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
    

    会话控制器 -

    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
    

    会话视图 -

    <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%>
    

    用户表 -

    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");
    

    路线 -

    Rails.application.routes.draw do
    
      resources :sessions
      resources :feedback_details
      resources :users
      resources :meetings
      resources :user_types
      root 'sessions#new'
    

    错误日志 -

    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)
    

    如果我需要提供更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

你在这里错过了一小部分代码。

class User < ActiveRecord::Base