gpt4 book ai didi

ruby-on-rails - 用户属性不会保存

转载 作者:太空宇宙 更新时间:2023-11-03 18:28:39 25 4
gpt4 key购买 nike

我的用户模型有一个名为“points”的属性,当我尝试在另一个模型 Controller (递减点)中更新它时,该属性不会保存(即使在将它添加到 attr_accessible 之后)。

我的 Venture Controller 代码中的方法:

def upvote
@venture = Venture.find(params[:id])
if current_user.points < UPVOTE_AMOUNT
flash[:error] = "Not enough points!"
else
flash[:success] = "Vote submitted!"
current_user.vote_for(@venture)
decremented = current_user.points - UPVOTE_AMOUNT
current_user.points = decremented
current_user.save
redirect_to :back
end

我什至尝试过使用 update_attributes 方法,但无济于事。

我用 flash 添加了一个快速的小测试,看看它是否正在保存:

if current_user.save
flash[:success] = "Yay"
else
flash[:error] = "No"
end

并返回错误。

current_user 来 self 的 session 助手:

def current_user
@current_user ||= user_from_remember_token
end

提前致谢。

我的用户模型:

class User < ActiveRecord::Base
attr_accessor :password, :points
attr_accessible :name, :email, :password, :password_confirmation, :points

STARTING_POINTS = 50

acts_as_voter
has_karma :ventures

has_many :ventures, :dependent => :destroy

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }

before_save :encrypt_password
after_initialize :initialize_points

def has_password?(submitted_password)
password_digest == encrypt(submitted_password)
end

def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil?
return user if user.has_password?(submitted_password)
end

def self.authenticate_with_salt(id, cookie_salt)
user = find_by_id(id)
(user && user.salt == cookie_salt) ? user : nil
end

private

def initialize_points
self.points = STARTING_POINTS
end

def encrypt_password
self.salt = make_salt if new_record?
self.password_digest = encrypt(password)
end

def encrypt(string)
secure_hash("#{salt}--#{string}")
end

def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end

def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end

这是我在打印 <%= debug current_user %> 后得到的结果

--- !ruby/object:User 
attributes:
id: 1
name: Test User
email: a@s.com
created_at: 2011-08-27 21:03:01.391918
updated_at: 2011-08-27 21:03:01.418370
password_digest: 40d5ed415df384adaa5182a5fe59964625f9e65a688bb3cc9e30b4eef2a0614b
salt: ac7a332f5d63bc6ad0f61ceacb66bc154e1cad1164fcaed6189d8cea2b55ffe4
admin: t
points: 50
longitude_user:
latitude_user:
attributes_cache: {}

changed_attributes: {}

destroyed: false
errors: !omap []

marked_for_destruction: false
new_record: false
points: 50
previously_changed: {}

readonly: false

最佳答案

您要求在每次保存用户时都提供用户密码。提交点赞时,密码不存在,因此验证未通过。

关于ruby-on-rails - 用户属性不会保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7218075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com