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.
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].
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.