update_attribute #<Class 的错误未定义方法 `update_attribute'

update_attribute with error undefined method `update_attribute' for #<Class

下面的具体代码来自书上使用Rails4 我相信 rails 5 需要强大的参数,这可能会导致 Rails 5 出错。 请任何人都可以分享我。如何为以下代码申请许可证和要求)

来自模特user.rb

def remember
    remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
end

错误(在 rails 5 中)

NoMethodError in SessionsController#create
undefined method `update_attribute' for #<Class

这是我所做的。

def remember
    remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(params:[remember_token]))
end

错误:

NoMethodError in SessionsController#create
undefined method `update_attribute' for #<Class:0x007fd23b30a790> Did you mean? _default_attributes

要清楚:从 sessionshelper.erb

调用的那些方法
module SessionsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  # Remembers a user in a persistent session.
  def remember(user)
    User.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end


    # Returns the user corresponding to the remember token cookie.
  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
      user = User.find_by(id: user_id)
      if user && user.authenticated?(cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end
  end


  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end

# Forgets a persistent session.
  def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end
  # Logs out the current user.
  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end
end

user.rb

class User < ApplicationRecord
  attr_accessor :remember_token, :activation_token, :reset_token, :remember_digest
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password   
  validates :password, presence: true, length: { minimum: 6 }
class << self
  # Returns the hash digest of the given string.
  def digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  def remember
    remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end



  # Returns a random token.
  def new_token
    SecureRandom.urlsafe_base64
  end
end
  # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end
  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

  def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
  end

end

sessionscontroller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      remember user
      redirect_to user
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    log_out
    redirect_to url_for(:controller => :sessions, :action => :new)  
  end
end

我有一个类似的实现,也许这对你有帮助

user.rb

  def create_confimation_token
    generate_token(:confirmation_token)
    update_attribute(:expiration,Time.zone.now + 2.days)
    save!  
  end

  def generate_token(column)
      begin
        self[column] = SecureRandom.urlsafe_base64
      end 
  end   

问题出在帮助程序,行

您正在使用 User class 调用 remember 方法,因此它显示 undefined method for class。而是调用 user object

User.remember

应该是,

user.remember

def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
end

您应该在 user 上呼叫 update_attribute 尝试

current_user.update_attribute(a: b)

在你的SessionsController#create

update_attributes 是实例方法而不是 class 方法,因此首先需要在 User class.

的实例上调用它

喜欢:

 @user.update_attributes(:remember_digest, User.digest(params:[remember_token]))

试试这个。

你应该为用户对象调用 update_attributes。

helper.rb

  def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end

model.rb

  def remember
    remember_token = new_token
    self.update_attributes(:remember_digest, digest(remember_token))
  end

现在有效

发生的事情是

  1. class << user.rb
  2. 中的自己

导致以下方法无效。

def remember
  remember_token = User.new_token
  update_attribute(:remember_digest, User.digest(remember_token))
end
    SessionsHelper 中的
  1. User.remember 应该是

    user.remember

谢谢大家