Rails 从 OrdersController 更新用户模型的属性

发布于 2024-10-31 06:36:03 字数 2466 浏览 1 评论 0原文

这是我的代码:

类 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 的用户模型更新属性?

简而言之,是否无法从模型#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:用户模型

需要“摘要”

类 User < ActiveRecord::基

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,我发现它正在尝试在此请求期间验证密码的存在。一旦我注释掉它,它就毫无问题地保存了。我不确定为什么会发生这种情况,但我会从这里开始。谢谢大家,特别是。德马克!

This my code:

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

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?

Simply put, is it not possible to update an attribute of model #1 from a controller of model #2?

Thanks

UPDATE:
With the help of the men below, I was able to verify two things:
1. result = work.params ['billingid'] returns string
2. That I am able to save into a different model from any controller

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.

I ran,

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

and it was successful. But,

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

This fails even though attr_accessible is set correctly. What else could prevent from saving certain attributes other than attr_accessible? Thanks everyone!

UPDATE 2: User Model

require 'digest'

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

end
end

UPDATE FINAL: SOLUTION
using @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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2024-11-07 06:36:03

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

  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.save 返回 false 时,请务必检查 user.errors

There should be no issue updating any number of models from a controller.

  1. Make sure that work.params['billingid'] actually contains a value.

  2. 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)

Edit: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文