Hi,
i want to know how to use method from model to be used in
controller like
sale.rb model
def list
some stuff
end
employee controller
def show
@stamp = Sale.list
end
which shows me following error
undefined method `list’
how to over come this problem?
thanks,
-pab
Pab
2
You are calling list as a class method so you need to define it as a
class method
def self.list
some stuff
end
or you need to call it as an instance method
s = Sale.new
@stamp = s.list
or even
@stamp = Sale.new.list
Pab
3
On 14 October 2011 14:35, Pab [email protected] wrote:
hi,
still same error comes
undefined method ‘list’
Show us the current code and the error please.
Also please remember to quote the previous reply so we know what you
are replying to.
Colin
Pab
4
hi,
still same error comes
undefined method ‘list’
thanks,
-pab
Pab
5
hi,
in employee controller
def show
@stamp=Sale.new.list
end
in sale.rb model i am using
def self.list
@list=Sale.find(:all)
return @list
end
its shows following error
undefined method ‘list’
thanks,
-pab
Pab
6
Now you have the opposite problem… You are now calling list as an
instance
method and you have it defined as a class method.
You need to do ONE of the following.
Define it as a class method, and call it as a class method:
def self.list
code
end
@list = Sale.list
Define it as an instance method and call it as an instance method:
def list
code
end
@list = Sale.new.list
However, based on what the list method does, it’s probably best that you
do
the former and have it be a class method.
Pab
7
@Tim thanks for your help, your idea works fine
thanks,
-pab
Pab
8
Pab wrote in post #1027023:
@Tim thanks for your help, your idea works fine
thanks,
-pab
Thanks u all
Regards
Fahim Babar Patel