Ruby on rails 为什么此方法在我的控制器中不起作用?-轨道3

Ruby on rails 为什么此方法在我的控制器中不起作用?-轨道3,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,这与这个问题有关: 但它实际上有点不同,因为它涉及到我的控制器中的特定操作 这是控制器操作(upload\u controller.rb): 这是我的User.rb型号: # == Schema Information # Schema version: 20110412170916 # # Table name: users # # id :integer not null, primary key # email

这与这个问题有关:

但它实际上有点不同,因为它涉及到我的控制器中的特定操作

这是控制器操作(
upload\u controller.rb
):

这是我的
User.rb
型号:

# == Schema Information
# Schema version: 20110412170916
#
# Table name: users
#
#  id                   :integer         not null, primary key
#  email                :string(255)
#  encrypted_password   :string(128)
#  password_salt        :string(255)
#  reset_password_token :string(255)
#  remember_token       :string(255)
#  remember_created_at  :datetime
#  sign_in_count        :integer
#  current_sign_in_at   :datetime
#  last_sign_in_at      :datetime
#  current_sign_in_ip   :string(255)
#  last_sign_in_ip      :string(255)
#  username             :string(255)
#  first_name           :string(255)
#  last_name            :string(255)
#  created_at           :datetime
#  updated_at           :datetime
#  invitation_token     :string(60)
#  invitation_sent_at   :datetime
#  plan_id              :integer
#  current_state        :string(255)
#  confirmation_token   :string(255)
#  confirmed_at         :datetime
#  confirmation_sent_at :datetime
#  space_used           :integer         default(0), not null
#  failed_attempts      :integer         default(0)
#  unlock_token         :string(255)
#  locked_at            :datetime
#  trial_end_date       :date
#  active_subscription  :boolean
#

class User < ActiveRecord::Base
    acts_as_voter
  devise :database_authenticatable, :confirmable, :registerable, :timeoutable,
         :recoverable, :rememberable, :trackable, :validatable, :invitable, :lockable


  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, :last_name, :plan_id
  has_friendly_id :username, :use_slug => true, :strip_non_ascii => true

    before_save :update_space

  has_many :uploads

  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end

  def update_space
       total_size = 0
         if self.uploads.count > 0
           self.uploads.each do |upload|
             total_size += upload[:image_file_size]
           end
         end
    self.space_used = total_size
  end

    def space_left
        (self.plan.storage * 1024 * 1024 * 1024) - self.space_used.to_f     
    end

end
#==架构信息
#架构版本:20110412170916
#
#表名:用户
#
#id:整数不为空,主键
#电子邮件:string(255)
#加密密码:字符串(128)
#密码:字符串(255)
#重置密码\u令牌:字符串(255)
#记住\u标记:字符串(255)
#还记得在:datetime创建的吗
#计数中的符号:整数
#当前登录时间:datetime
#上次登录时间:datetime
#ip中的当前符号:字符串(255)
#ip中的最后一个签名:字符串(255)
#用户名:字符串(255)
#名字:字符串(255)
#姓氏:字符串(255)
#创建时间:datetime
#更新时间:datetime
#邀请令牌:字符串(60)
#邀请发送时间:datetime
#计划id:整数
#当前状态:字符串(255)
#确认令牌:字符串(255)
#确认时间:datetime
#确认发送时间:datetime
#使用的空间:整数默认值(0),不为空
#失败的\u尝试:整数默认值(0)
#解锁令牌:字符串(255)
#锁定时间:datetime
#审判结束日期:日期
#活动订阅:布尔值
#
类用户true,:strip\u non\u ascii=>true
保存前:更新空间
有很多:上传
定义角色符号
roles.map do | role|
role.name.下划线.to_sym
结束
结束
def update_空间
总尺寸=0
如果self.uploads.count>0
self.uploads.each do| upload|
总大小+=上传[:图像大小]
结束
结束
self.space\u used=总大小
结束
def空间_左
(self.plan.storage*1024*1024*1024)-self.space\u用于
结束
结束
有问题的行位于
upload\u controller.rb
中,其中显示
current\u user.update\u space
。当上载被销毁时,不会更新当前登录的用户


如何实现这一点?

您正在尝试在控制器响应后运行代码。要移动此行:

 current_user.update_space

在上面的
响应
呼叫。

您正试图在控制器响应后运行代码。要移动此行:

 current_user.update_space

上面的
响应_to
呼叫。

由于某些原因,这仍然不起作用。是不是方法
update\u space
是在
User
模型中声明的,而这是在
upload\u controller
中调用的?@marcamillion:不,这与此无关。控制器和模型仅按名称链接。控制器可以访问任何模型。您的
update\u space
方法在更新空间后未保存用户记录。您需要在模型末尾调用
save
。我试过了,得到了
!!处理请求时出现意外错误:死锁;递归锁定
。我想这是因为在我的用户模型中,我在保存之前调用
update\u space
。因此,通过将
self.save
添加到我的
update\u space
方法,它将所有东西都扔进了疯狂小镇。因此,我从我的
用户
模型中删除了
保存之前的
,并重新调整了一些内容。现在空间在每次上传和删除后都会更新-这正是我想要的。由于某些原因,这仍然不起作用。是不是方法
update\u space
是在
User
模型中声明的,而这是在
upload\u controller
中调用的?@marcamillion:不,这与此无关。控制器和模型仅按名称链接。控制器可以访问任何模型。您的
update\u space
方法在更新空间后未保存用户记录。您需要在模型末尾调用
save
。我试过了,得到了
!!处理请求时出现意外错误:死锁;递归锁定
。我想这是因为在我的用户模型中,我在保存之前调用
update\u space
。因此,通过将
self.save
添加到我的
update\u space
方法,它将所有东西都扔进了疯狂小镇。因此,我从我的
用户
模型中删除了
保存之前的
,并重新调整了一些内容。现在,每次上传和删除后,空间都会更新,这正是我想要的。