Undefined method 'menu'

hi im new in ROR development and starting a reservation system project
i am to show the menu that the customer added in the package that he
choose and inside that menu the recipes of the current menu but it rises
me a

undefined method ‘menu’ error

i belive my associations of my model is right but you may correct it if
its wrong thank

package_line_item.rb

belongs_to :menu
belongs_to :reservation

reservation.rb

has_one :reservation_package
belongs_to :service
has_many :reservation_function_rooms
has_many :package_line_items
has_many :menus , :through => :package_line_items, :uniq => true
has_many :function_rooms, :through =>:reservation_function_rooms

menu.rb

has_many :package_line_items
has_many :menu_recipes
has_many :recipes, :through => :menu_recipes, :uniq => true
belongs_to :menu_category

package_line_item_controller.rb

def index
@package_line_items = PackageLineItems.all
end

def show
@reservation = Reservation.includes(:package_line_items =>
:menu).find(params[:id])
end

def new
@reservation = Reservation.find(params[:reservation_id])
@package_line_item = @reservation.package_line_items.build
end

def create
@reservation = Reservation.find(params[:reservation_id])
@reservation.package_line_items.build(params[:package_line_item])

if @package_line_item.save
  redirect_to @reservation ,:notice => "added menu"
end

routes.rb

resources :services

resources :reservations do
resources :reservation_packages
resources :reservation_function_rooms
resources :packages
resources :package_line_items
resources :package_crews

end

resources :function_rooms

resources :crews

resources :menu_categories

resources :menus do
resources :menu_recipes
end

ActiveAdmin.routes(self)

devise_for :admin_users, ActiveAdmin::Devise.config

resources :recipe_categories

resources :recipes

package_line_item/show.html.erb

<%= notice %>


<%= @reservation.package_line_items.menu.name%>

i tried on my show.html.erb file
<%= @reservation.package_line_items.first.menu.name%>

but it only returns the first menu that i added the when i open the
second menu the information inside it is the the information in my first
menu

if other file is needed feel free to ask me thank you more power to us
thanks

On 25 March 2012 07:40, Doe J. [email protected] wrote:

package_line_item.rb
has_many :menus , :through => :package_line_items, :uniq => true

redirect_to @reservation ,:notice => “added menu”
resources :package_line_items
resources :menu_categories

resources :recipes

package_line_item/show.html.erb

<%= notice %>

<%= @reservation.package_line_items.menu.name%>

You have not actually said but I presume the error is on the line
above. The error says that there is no method menu, consider what it
is that you are trying to call this method on:
@reservation.package_line_items
What is the type of that? The clue is that it is plural, so it is
probably a collection of items. You can’t call menu on a collection
of items, you will have to select one at a time.


i tried on my show.html.erb file
<%= @reservation.package_line_items.first.menu.name%>

but it only returns the first menu that i added the when i open the
second menu the information inside it is the the information in my first
menu

Right, that is because you are now calling it on the first such item.
If you want to show information for all the items you will have to
loop round them displaying the menu names.

If you don’t know how to do that then I suggest working through some
good tutorials such as railstutorial.org (which is free to use online)
which will give you a better understanding of Ruby and Rails.

Colin

Colin L. wrote in post #1053187:

On 25 March 2012 07:40, Doe J. [email protected] wrote:

package_line_item.rb
has_many :menus , :through => :package_line_items, :uniq => true

redirect_to @reservation ,:notice => “added menu”
resources :package_line_items
resources :menu_categories

resources :recipes

package_line_item/show.html.erb

<%= notice %>

<%= @reservation.package_line_items.menu.name%>

You have not actually said but I presume the error is on the line
above. The error says that there is no method menu, consider what it
is that you are trying to call this method on:
@reservation.package_line_items
What is the type of that? The clue is that it is plural, so it is
probably a collection of items. You can’t call menu on a collection
of items, you will have to select one at a time.


i tried on my show.html.erb file
<%= @reservation.package_line_items.first.menu.name%>

but it only returns the first menu that i added the when i open the
second menu the information inside it is the the information in my first
menu

Right, that is because you are now calling it on the first such item.
If you want to show information for all the items you will have to
loop round them displaying the menu names.

If you don’t know how to do that then I suggest working through some
good tutorials such as railstutorial.org (which is free to use online)
which will give you a better understanding of Ruby and Rails.

Colin

thanks mr colin sir
i tried looping with

<% @reservation.package_line_items.each do |menu|%>

<%= menu.menu.name%>

<%end%>

but it is displaying all of the menus that the reservation not the
specific menu that i clicked

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs