# Instance variable?

**URL:** <https://www.ruby-forum.com/t/instance-variable/52947>\
**Category:** Rails\
**Created:** [January 21, 2006, 12:21am UTC](https://www.ruby-forum.com/t/instance-variable/52947 "2006-01-21T00:21:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 21, 2006, 12:21am UTC](https://www.ruby-forum.com/t/instance-variable/52947/1 "2006-01-21T00:21:04Z")

</div>

I’m trying to grab a temporary variable from my view and then pass along  
that value to my model

My view code is this:

\<%= text\_field 'product', 'qty\_added', {:size=\>3}

And I’m trying to pass the qty\_added variable to my model.  
item.quantity = qty\_added

It doesn’t seem to be working… I’m sure I’m missing many fundamental  
things but if somebody could start me down the correct path…

Thx…

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 21, 2006, 1:36am UTC](https://www.ruby-forum.com/t/instance-variable/52947/2 "2006-01-21T01:36:00Z")

</div>

Vince:

There is probably some clever way of doing what you ask. For myself,  
controllers are the middlemen, so my views talk to my controllers and  
my controllers talk to my model. Does that help?

bruce

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 21, 2006, 1:41am UTC](https://www.ruby-forum.com/t/instance-variable/52947/3 "2006-01-21T01:41:01Z")

</div>

> There is probably some clever way of doing what you ask. For myself,  
> controllers are the middlemen, so my views talk to my controllers and  
> my controllers talk to my model. Does that help?

Hi Bruce,

I’m very new to the whole programming thing so yes it does help. I’m  
still kind of lost though…

In this line,

\<%= text\_field ‘product’,  
‘qty\_added’,  
{:size=\>3}

I don’t even seem to be correctly capturing any data. I have a  
display\_cart.rhtml file where I print the value of that variable using

```
<%= @qty_added.inspect %>
```

But that always comes up as ‘nil’

I figure once I get the qty\_added stored correctly I can then work on  
the controller. Does that sound right?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 21, 2006, 2:19am UTC](https://www.ruby-forum.com/t/instance-variable/52947/4 "2006-01-21T02:19:34Z")

</div>

One piece of added information to the mix. I think the big issue is  
that where I submit the data I am not passing the qty\_added. Here is  
that relevant code:

```
    <%= link_to 'Add to Cart',
                {:action => 'add_to_cart', :id => product },
                :class => 'addtocart' %><br/>

```

I’ve defined the form like this:

\<%=  
text\_field ‘product’, ‘qty\_added’, {:size=\>3}

What should I modify in the ‘Add to Cart’ link to also capture the  
quantity?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 21, 2006, 4:42am UTC](https://www.ruby-forum.com/t/instance-variable/52947/5 "2006-01-21T04:42:58Z")

</div>

This code implies that you are using a GET method to submit your data.  
Even inside a form, a link\_to will create an \<a href … \> link. Look at  
your logs (log/development.log) and see what the params hash has in it  
when the action is invoked (you are running tail -f log/development.log,  
right? :).

If I understand the original problem, you have a “meta-field” called  
qty\_added that is not a database column. Its behavior is to exist solely  
to add to the quantity column that is in the database. Rails won’t  
automatically know what to do when that unknown field comes back in the  
params hash, so you will have to manually extract it and stuff it in the  
model. E.g.

# controller code

def addtocart  
thingie = Thing.find(params[:id])  
thingie.quantity += params[:product][‘qty\_added’] # assuming you  
wanted to add…  
begin  
thingie.save!  
rescue  
# take some corrective action.  
end  
end

Alternatively, you can define the behavior in your model by adding  
accessors for qty\_added and making the write accessor add to quantity.

Does this help? Does anyone else have thoughts on this?

Vince W. wrote:

> One piece of added information to the mix. I think the big issue is  
> that where I submit the data I am not passing the qty\_added. Here is  
> that relevant code:
> 
> ```
> <%= link_to 'Add to Cart',
> {:action => 'add_to_cart', :id => product },
> :class => 'addtocart' %><br/>
> 
> ```
> 
> I’ve defined the form like this:
> 
> \<%=  
> text\_field ‘product’, ‘qty\_added’, {:size=\>3}
> 
> What should I modify in the ‘Add to Cart’ link to also capture the  
> quantity?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [January 21, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/instance-variable/52947/6 "2006-01-21T16:01:28Z")

</div>

Steve R. wrote:

> This code implies that you are using a GET method to submit your data.  
> Even inside a form, a link\_to will create an \<a href … \> link. Look at  
> your logs (log/development.log) and see what the params hash has in it  
> when the action is invoked (you are running tail -f log/development.log,  
> right? :).

I suppose it is a get. I tried using a form instead just to see if it  
would help, but I’m having trouble accessing variables from database in  
the form. For instance, in \_form.rhtml I have code that says:

\<%= h(product.title) %\>

But that simple code gives me an “undefined local variable or method  
`product’ for #\<#Class:0x4081f164:0x4081f0b0\>” error

> If I understand the original problem, you have a “meta-field” called  
> qty\_added that is not a database column.

That’s correct. In my original model I had a line setting quantity  
manually. i.e. item.quantity = 1. I want to change that to  
item.quantity = qty\_added but can’t seem to pass the variable to my  
controller.

> Its behavior is to exist solely  
> to add to the quantity column that is in the database. Rails won’t  
> automatically know what to do when that unknown field comes back in the  
> params hash, so you will have to manually extract it and stuff it in the  
> model. E.g.
> 
> # controller code
> 
> def addtocart  
> thingie = Thing.find(params[:id])  
> thingie.quantity += params[:product][‘qty\_added’] # assuming you  
> wanted to add…  
> begin  
> thingie.save!  
> rescue  
> # take some corrective action.  
> end  
> end

I tried the above but got an undefined method quantity error. I then  
tried adding attr\_accessor to my models for both product.rb and  
line\_item.rb but that didn’t solve it. Couldn’t really figure out where  
to define quantity to make it happy.

> Alternatively, you can define the behavior in your model by adding  
> accessors for qty\_added and making the write accessor add to quantity.

I would be happy to try this too… really I’m just looking for the  
easiest solution. :ol

> Does this help? Does anyone else have thoughts on this?

I’m starting to understand things better… I appreciate your help…  
please keep throwing suggestions at me!!!

-Vince
