简体   繁体   中英

Rails updating attributes of a User Model from OrdersController

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!

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM