-2

我正在尝试在 RAILS 上创建一个身份验证系统find_by_user_name_and_password()

我有以下问题:

undefined method `find_by_user_name_and_password' for #<Class:0x007fae373d5698>
Rails.root: /ror/blog/Blog`

Application Trace | Framework Trace | Full Trace
app/controllers/sessions_controller.rb:7:in `create'

这是我的 session_controller 代码

    class SessionsController < ApplicationController
    skip_before_filter :authorizes
  def new
  end

  def create
        user = User.find_by_user_name_and_password(params[:name], params[:password]) 
        if user
            session[:user_id] = user.id
            redirect_to admin_url
        else
            redirect_to login_url, alert: "Bad datas"
        end
  end

  def destroy
    session[:user_id] = nil
    rederect_to blog_url, notice: "End seans"
  end
end

模型定义:

class User < ActiveRecord::Base
  attr_accessible :name, :password_digest, :password_confirmation
  validates :name, presence: true, uniqueness: true
    #validates_confirmation_of :password  
  has_secure_password
end

迁移

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :password_digest

      t.timestamps
    end
  end
end
4

1 回答 1

0

查看您刚刚发布的模型,我发现您没有密码字段......所以没有find_by_whatever_and_password

你需要使用

user = User.find_by_name(params[:user][:name])
if user && user.authenticate(params[:user][:password])
  # etc.
于 2013-02-07T16:25:39.463 回答