# Rails and Checkboxes

**URL:** <https://www.ruby-forum.com/t/rails-and-checkboxes/65350>\
**Category:** Rails\
**Created:** [August 17, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/rails-and-checkboxes/65350 "2006-08-17T17:37:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [August 17, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/rails-and-checkboxes/65350/1 "2006-08-17T17:37:54Z")

</div>

Hi

I’m having a really hard time trying to get a specific form in RoR to  
work. I’m trying to build a really simple page which lets the user make  
a pizza by setting check boxes. The choose their toppings, click submit  
and the form does a callback and shows them how much it will cost to  
have a pizza with those toppings on.

At the moment I have the following:

View:

\<%= start\_form\_tag :action =\> “pick\_your\_own” %\>

Anchovies  
\<%= check\_box “pizza”, “anchovies” %\>

\<% end\_form\_tag %\>

Controller:

@total = 0

if @pizza.anchovies? \> 0 then  
@total = @total + 10  
end

This gives me a NoMethodError with “You have a nil object when ou didn’t  
expect it. This error occured when evaluating nil.anchovies?”

Also note, I’m not trying to enter this into a database, I’m just trying  
to tell the user how much this will cost. I’ve been trawling with google  
for hours. I can’t seem to find any answers that work.

Any help with this would be great! Thanks.

---

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [August 17, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/rails-and-checkboxes/65350/2 "2006-08-17T17:44:41Z")

</div>

On 8/17/06, Feanix [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= start\_form\_tag :action =\> “pick\_your\_own” %\>  
> if @pizza.anchovies? \> 0 then  
> @total = @total + 10  
> end
> 
> This gives me a NoMethodError with “You have a nil object when ou didn’t  
> expect it. This error occured when evaluating nil.anchovies?”

Can you post your code that shows where you populate your @pizza  
object with values from params?

– James

---

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [August 17, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/rails-and-checkboxes/65350/3 "2006-08-17T17:50:09Z")

</div>

> Can you post your code that shows where you populate your @pizza  
> object with values from params?

Ah. I haven’t that’s pretty embarrasing. Well, if you didn’t know I was  
a newb before… 😛

Anyways, I’m not sure how to go about populating it? Something like  
this?

@pizza = Pizza.new(@params[\<form name goes here?\>])

---

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [August 17, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/rails-and-checkboxes/65350/4 "2006-08-17T18:39:02Z")

</div>

I think I’ve been barking up the wrong tree all day. The following seems  
to get me what i want:

View

\<%= start\_form\_tag :action =\> “pick your own” %\>

Anchovies:  
\<%= check\_box\_tag “anchovies” %\>

\<%= end\_form\_tag %\>

Controller

@total = 0

@anchovies = params[:anchovies]

if (@anchovies) then  
@total = @total + 5  
end

That _seems_ be working. Yay! 🙂

More experimentation is needed to make sure it can work for multiple  
values. I wonder if I can’t manage a more succinct conditional too 😕

---

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [August 17, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/rails-and-checkboxes/65350/5 "2006-08-17T18:47:48Z")

</div>

On 8/17/06, Feanix [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> More experimentation is needed to make sure it can work for multiple  
> values. I wonder if I can’t manage a more succinct conditional too 😕

@total = @total + 5 if @anchovies

One tip that may help is that your logs will contain every “params”  
value when you submit a form. This lets you see the names you should  
be using and will help a lot the first time you run into a situation  
where you have to use params[:some\_name][:some\_nested\_name].

---

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [August 17, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/rails-and-checkboxes/65350/6 "2006-08-17T20:01:08Z")

</div>

Great, thanks for your help James.

---

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [August 17, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/rails-and-checkboxes/65350/7 "2006-08-17T18:17:45Z")

</div>

Hi Feanix,

Feanix wrote:

> View:
> 
> \<%= start\_form\_tag :action =\> “pick\_your\_own” %\>  
> Anchovies  
> \<%= check\_box “pizza”, “anchovies” %\>  
> \<% end\_form\_tag %\>
> 
> Controller:
> 
> @total = 0

(you need to add something like…)

@pizza = FoodItem.new

if params[:pizza][:anchovies] == ‘y’  
@pizza.anchovies = 1  
end

> if @pizza.anchovies? \> 0 then  
> @total = @total + 10  
> end

Even though you’re not trying to store the info in the database, since  
@pizza is not a simple type, you have to let Rails know about the @pizza  
object and will, I think, need to define a model (FoodItem above) to do  
that.

hth,  
Bill
