Rails and Checkboxes

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.

On 8/17/06, Feanix [email protected] 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

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… :stuck_out_tongue:

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

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

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! :slight_smile:

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 :confused:

On 8/17/06, Feanix [email protected] 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 :confused:

@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].

Great, thanks for your help James.

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