# Problem with menu functionality

**URL:** <https://www.ruby-forum.com/t/problem-with-menu-functionality/121308>\
**Category:** Rails\
**Created:** [November 26, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/problem-with-menu-functionality/121308 "2007-11-26T18:35:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![weloki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@weloki](https://www.ruby-forum.com/u/weloki)\
**Post date:** [November 26, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/problem-with-menu-functionality/121308/1 "2007-11-26T18:35:27Z")

</div>

Hi, I’m trying to display separate menus based on records in database  
tables. The menus are situated within a form which separates the menus  
based on their service type. The ‘service’ table references the  
costcenter\_id so each service is either in category X or Y. Each menu is  
supposed to list all of the items in X or Y. Since there are only two  
records (X and Y) in the ‘costcenter’ table, only two menus should show  
up. The menus show up fine and are populated with the items correctly,  
but he problem is that the first menu works fine, but the second menu  
does nothing at all when I submit the form.  
Whenever I select an item in the second menu, what shows is the same  
error as if I had selected no data in the form and tried to submit it. I  
always see this error:

" NoMethodError in ConsultsController#create

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.charge\_rate"

Why would the first menu be able to see @var.charge\_rate but not the  
second?

* * *

## In the view:

 \<% @costcenters.each\_with\_index do |costcenter, index| %\> 
### \<%= costcenter.name %\>

 \> \<% @services.each do |service| %\> \<% if service.active != 0 %\> \<% if service.costcenter\_id == costcenter.id %\> \<%= service.name %\> selected \<% end %\> value="\<%= service.id %\>"\> \<%= service.name %\> \<% end %\> \<% end %\> \<% end %\> 
 \<% end %\>

* * *

## In the controller:

def create  
  
#the next 3 lines will get the charge rate  
serv\_id = @consult.service\_id  
@service = Service.find(:first, :conditions =\> [“id = ?”, serv\_id])  
@consult.charge\_rate = @service.charge\_rate  
@consult.total\_charge = Consult.totalCharge(@minutes,  
@consult.charge\_rate)  
if @consult.save  
flash[:notice] = ‘Consult was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end
