First thing if you want to modify the title on your product after
getting it with @product = Product.find(params[:id]) you cant do
“Product.title = “You don’t decide the title! I do!”” as “Product”
refers to your class and not to the object. You must refer the object
@product = Product.find(params[:id]) # Getting the product @product.title = “You don’t decide the title! I do!” # Updating title @product.save # Saving object in database
Another thing, if you have a undefined method `title=’ for #<Class:
0x365b654> error, it seems that your Product class don’t have a title
attribute. Check in the database that your Product table has a title
attribute before trying to update it ^^
But I got an error (undefined method `title=’ for #Class:0x365b654)
Could you help me?
Why would you want to do this in the controller (not through user
If it’s business logic, it belongs in the model.
You could do something like this in the Product model with callbacks.
class Product < ActiveRecord::base
self.title = “this is my title…”
Then when you do an @product.update_attributes(…) in your
controller, it’ll run this before it saves overriding anything passed
in the parameters. It’s generally bad-mojo to modify params during the
Additionally, if with ActiveRecord callbacks you can do this before
create, update, or save (depending on how you want to approach this.)
Learn more about callbacks here: