Forum: Ruby on Rails How to change a variable while editing a table? (scaffold)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-20 20:19
Hello everyone!

I'm trying to modify an example from a book. The example show how to
create a scaffold for a table.

But I want to change the way things are modified.

Here is the method in my controller :

  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        flash[:notice] = 'Product was successfully updated.'
        format.html { redirect_to(@product) }
        format.xml  { head :ok }
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product.errors, :status =>
:unprocessable_entity }

So I tried to add this line :

Product.title =  "You don't decide the title! I do!"

after @product = Product.find(params[:id])

But I got an error (undefined method `title=' for #<Class:0x365b654>)

Could you help me?

Thank you!
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-20 20:21
In fact I want to modify this parameters :

 "description"=>"je decide ou pas?@@@"}}
Ce14206b729afc6ac1d4c454eb434af0?d=identicon&s=25 CiriusMex (Guest)
on 2009-02-20 20:26
(Received via mailing list)
Hi Guillaume,

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
that way:

@product = Product.find(params[:id]) # Getting the product
@product.title =  "You don't decide the title! I do!" # Updating title # 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 ^^


On 20 feb, 13:19, Guillaume Loader <>
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-20 20:36
I've also tried your way before asking but the title is not modified...

Anyway, I've find a way to do that by adding this line :

params[:product][:title] = "I decide the title"

I don't really understand why by the way ^^

And thank you for your reply :)
A82ba1167f4d4a8d1de63820e576a87f?d=identicon&s=25 Robby Russell (Guest)
on 2009-02-20 20:44
(Received via mailing list)
On Fri, Feb 20, 2009 at 11:19 AM, Guillaume Loader
<> wrote:
>  def update
> :unprocessable_entity }
> But I got an error (undefined method `title=' for #<Class:0x365b654>)
> Could you help me?
> Thank you!

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

  before_save :set_title_my_way

  def set_title_my_way
    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:


Hope this helps!


Robby Russell
Chief Evangelist, Partner

design // development // hosting w/Ruby on Rails
aim: planetargon

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4068 [fax]
Fcb841958d28b10724b2dd4fc5a559a0?d=identicon&s=25 deepali_k (Guest)
on 2009-02-21 14:54
(Received via mailing list)
First you have to find the product by:
 @product = Product.find(params[:id])
 @product.title =  "You don't decide the title! I do!"

On Feb 21, 12:19 am, Guillaume Loader <rails-mailing-l...@andreas-
This topic is locked and can not be replied to.