【问题标题】:Rails updating attributes of a User Model from OrdersControllerRails 从 OrdersController 更新用户模型的属性
【发布时间】:2011-08-01 05:35:42
【问题描述】:

这是我的代码:

类 OrdersController

def create
  @order = Order.new(params[:order])
    if @order.purchase
      work = GATEWAY.store(credit_card, options)
      result = work.params['billingid']
      current_user.update_attributes(:billing_id => result)
    end
  end
end

billingid 通过运行 GATEWAY.store(credit_card, options) 返回 我正在尝试将返回的billingid 保存到用户模型中的:billing_id 列中。是否无法从非 UsersController 更新 User 模型的属性?

简单地说,是不是不能从模型#2的控制器更新模型#1的属性?

谢谢

更新: 在下面这些人的帮助下,我能够验证两件事: 1. result = work.params ['billingid'] 返回字符串 2. 我可以从任何控制器保存到不同的模型中

但是,即使我有 attr_accessible :billing_id 我仍然无法将结果保存到 User 表的 billing_id 列中。我成功地将结果保存在 Store 表的 store_name 列中,所以我不知道阻止我保存的用户模型是什么。

我跑了,

@mystore = Store.find(current_user)
@mystore.store_name = result            
@mystore.save

它成功了。但是,

@thisuser = User.find(current_user)
@thisuser.billing_id = result
@thisuser.save

即使 attr_accessible 设置正确,此操作也会失败。除了 attr_accessible 之外,还有什么可以阻止保存某些属性?谢谢大家!

更新 2:用户模型

需要“摘要”

类用户<:base>

has_one :store
has_many :products
attr_accessor :password
# attr_accessible was commented out completely just to check as well. Neither worked
attr_accessible :name, :email, :password, :password_confirmation, :username, :billing_id
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 } 

username_regex = /^([a-zA-Z0-9]{1,15})$/

before_save :encrypt_password

def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
end
private
def encrypt_password
  self.salt = make_salt if new_record?
  self.encrypted_password = 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

结束 结束

更新最终:解决方案 使用@thisusers.errors,我发现它在此请求期间试图验证密码的存在。一旦我将其注释掉,它就会毫无问题地保存。我不确定为什么会这样,但我会从这里开始。谢谢大家,尤其是。 dmarkow!

【问题讨论】:

  • 控制器实际上只是一种组织应用程序功能的方式,您可以在任何控制器中使用任何模型。上面显示的代码看起来不错,假设 current_user 是一个可访问的方法,它返回具有 billing_id 属性的模型实例(大概是 User)。

标签: ruby-on-rails update-attributes


【解决方案1】:

从控制器更新任意数量的模型应该没有问题。

  1. 确保work.params['billingid'] 实际包含一个值。

  2. 你的User 模型可能有一些属性标记为attr_accessible(因为你有current_user,我假设你有身份验证,这通常意味着需要默认保护模型的属性)。如果是这种情况,这意味着只有可以通过批量分配更改这些属性(例如,使用update_attributes)。将billing_id 添加到attr_accessible 的属性列表中,或者不使用批量分配。 (相反,您只需先执行current_user.billing_id = result,然后执行current_user.save

编辑:问题最终是User 模型上的验证错误。当 user.save 返回 false 时,请务必检查 user.errors

【讨论】:

  • 嗨,dmarkow,我能够验证这两件事。我有 attr _accessible :billing_id 甚至尝试没有任何 attr_accessible。我尝试将它保存在 Store 模型的 store_name 列中,它实际上保存了!但我试图将其保存在 User 模型的 billing_id 列中,但它不会这样做。你能想到除了 attr_accessible 之外它不会保存到某个模型的任何其他原因吗?谢谢
  • 如果您的问题还没有解决,为什么要接受答案?我认为 dmarkow 是对的……向我们展示您的用户模型代码。
  • 那么@thisuser.save 行实际上是否失败(即它是否返回false)?
  • 是的,此行失败并返回 false。 @mystore.save 返回 true
  • 因此,如果它返回 false,下一步将是查看 @thisuser.errors 并查看导致它失败的原因。这可能是您的验证之一。
猜你喜欢
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多