问题描述
这是我的代码:
OrdersController类
class 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
通过运行GATEWAY.store(credit_card, options)
返回
billingid
我正在尝试将此返回的billingid
保存到用户模型的:billing_id
列中.是否可以从不是UsersController的用户模型更新属性?
billingid
is returned by running GATEWAY.store(credit_card, options)
I am trying to save this returned billingid
into :billing_id
column in User Model. Is it not possible to update attribute of User model from a that is not UsersController?
简单地说,是否无法从模型#2的控制器更新模型#1的属性?
Simply put, is it not possible to update an attribute of model #1 from a controller of model #2?
谢谢
更新:在以下人员的帮助下,我得以验证两件事:1.结果= work.params ['billingid']返回字符串2.我能够将其保存为与任何控制器不同的模型
UPDATE:With the help of the men below, I was able to verify two things:1. result = work.params ['billingid'] returns string2. That I am able to save into a different model from any controller
但是,即使我有attr_accessible:billing_id,我仍然无法将结果保存到User表的billing_id列中.我成功地将结果保存在Store表的store_name列中,所以我不知道阻止用户保存的用户模型是什么.
However, even though I have attr_accessible :billing_id I am still unable to save the result into billing_id column of User table. I was successful in saving the result in a store_name column of a Store table, so I don't know what it is about User model that is preventing me from saving.
我跑了,
@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以外的某些属性的?谢谢大家!
This fails even though attr_accessible is set correctly. What else could prevent from saving certain attributes other than attr_accessible? Thanks everyone!
更新2:用户模型
需要摘要"
类用户< ActiveRecord :: Base
class User < ActiveRecord::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!
UPDATE FINAL: SOLUTIONusing @thisusers.errors, I was able to find out that it was trying to validate the presence of password during this request. Once I commented it out, it saved without an issue. I am unsure why this is happening, but I will take it from here. Thanks everyone esp. dmarkow!
推荐答案
从控制器更新任何数量的模型应该没有问题.
There should be no issue updating any number of models from a controller.
-
确保
work.params['billingid']
实际上包含一个值.
您的User
模型可能具有一些标记为attr_accessible
的属性(由于您具有current_user
,所以我假设您已通过身份验证,这通常意味着默认情况下需要保护模型的属性).如果是这种情况,则意味着只能通过批量分配(例如,使用update_attributes
)更改仅这些属性.将billing_id
添加到attr_accessible
的属性列表中,或者不使用批量分配. (相反,您只需先执行current_user.billing_id = result
然后执行current_user.save
)
Your User
model may have some attributes marked as attr_accessible
(since you have current_user
, I assume you have authentication, and this often means needing to protect your model's attributes by default). If this is the case, that means that only those attributes can be changed by mass assignment (e.g. using update_attributes
). Either add billing_id
to the list of attributes that are attr_accessible
, or don't use mass assignment. (Instead, you would just do current_user.billing_id = result
and then current_user.save
)
编辑:该问题最终是对User
模型的验证错误.当user.save
返回false时,请务必确保检查user.errors
.
The problem wound up being a validation error on the User
model. Always make sure to check the user.errors
when user.save
returns false.
这篇关于Rails从OrdersController更新用户模型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!