# How to change a variable while editing a table? (scaffold)

**URL:** <https://www.ruby-forum.com/t/how-to-change-a-variable-while-editing-a-table-scaffold/160902>\
**Category:** Rails\
**Created:** [February 20, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/how-to-change-a-variable-while-editing-a-table-scaffold/160902 "2009-02-20T20:19:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 20, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/how-to-change-a-variable-while-editing-a-table-scaffold/160902/1 "2009-02-20T20:19:08Z")

</div>

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 }
  else
    format.html { render :action => "edit" }
    format.xml { render :xml => @product.errors, :status =>

```

:unprocessable\_entity }  
end  
end  
end

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!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 20, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/how-to-change-a-variable-while-editing-a-table-scaffold/160902/2 "2009-02-20T20:21:53Z")

</div>

In fact I want to modify this parameters :

{“commit”=\>“Update”,  
“\_method”=\>“put”,  
“authenticity\_token”=\>“31147ee98ead230e953389a3b8d35415162fca34”,  
“id”=\>“1”,  
“product”=\>{“title”=\>“test”,  
“image\_url”=\>“gifffff”,  
“description”=\>“je decide ou pas?@@@”}}

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 20, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/how-to-change-a-variable-while-editing-a-table-scaffold/160902/3 "2009-02-20T20:26:41Z")

</div>

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  
@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 ^^

Cheers,  
Olivier.

On 20 feb, 13:19, Guillaume L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 20, 2009, 8:36pm UTC](https://www.ruby-forum.com/t/how-to-change-a-variable-while-editing-a-table-scaffold/160902/4 "2009-02-20T20:36:51Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 21, 2009, 2:54pm UTC](https://www.ruby-forum.com/t/how-to-change-a-variable-while-editing-a-table-scaffold/160902/5 "2009-02-21T14:54:25Z")

</div>

First you have to find the product by:  
@product = Product.find(params[:id])  
@product.title = “You don’t decide the title! I do!”  
@product.save

On Feb 21, 12:19 am, Guillaume L. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 20, 2009, 8:44pm UTC](https://www.ruby-forum.com/t/how-to-change-a-variable-while-editing-a-table-scaffold/160902/6 "2009-02-20T20:44:19Z")

</div>

On Fri, Feb 20, 2009 at 11:19 AM, Guillaume L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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  
input?)

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

protected  
def set\_title\_my\_way  
self.title = “this is my title…”  
end  
end

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  
request.

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:

- [ActiveRecord::Callbacks](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)

Hope this helps!

Cheers,  
Robby

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting w/Ruby on Rails

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://robbyonrails.com/](http://robbyonrails.com/)  
[http://twitter.com/planetargon](http://twitter.com/planetargon)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]
