There are a number of things wrong with the code here:
@cust = Customer.find_by_id(params[:customer_id])
upd = @cust.update_attributes({:city_id => params[:city],
:zip_code => params[:zip],
:first_name => params[:first_name],
:last_name => params[:last_name],
:phone => params[:phone].each { |s| s.gsub!(/\D+/,'')},
:street1 => params[:street1],
:street2 => params[:street2],
:email => params[:email]
})
- It's currently in a controller action - I should move it to the Customer class
- My params don't exactly match the keys in my collection (MongoDB) - but it's a bit too late for me to deal with that...
- I use the .each with a block inside the update, I feel like I should separate that into it's own method
- While I don't explicitly call save, it does actually save. The documentation on update_attributes says that I should call save explicitly or it won't...
Here are some questions:
- What is the right way to move this into the model such that it will return TRUE or FALSE so that my controller can then set a flash[:success] or flash[:error] ?
- How do I normalize my params so that I can write ONE UNIVERSAL update method like @cust.update_attributes(params) which will intelligently update only non-nil params ?
- Is it absolutely worth the time to go back to the view and normalize the name attributes of each input to match the param?
I have other methods that update more or less params depending on the view, and I want to just have a single method "update_customer" that will take any old params hash with any values and only update the values that exist.