how call method from model to controller in rails 3

20 views
Skip to first unread message

Pab

unread,
Oct 14, 2011, 8:14:42 AM10/14/11
to Ruby on Rails: Talk
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

Peter Hickman

unread,
Oct 14, 2011, 8:23:41 AM10/14/11
to rubyonra...@googlegroups.com
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

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

Pab

unread,
Oct 14, 2011, 9:35:25 AM10/14/11
to Ruby on Rails: Talk

hi,

still same error comes
undefined method 'list'

thanks,
-pab

Colin Law

unread,
Oct 14, 2011, 9:44:35 AM10/14/11
to rubyonra...@googlegroups.com
On 14 October 2011 14:35, Pab <prabu...@gmail.com> 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

unread,
Oct 14, 2011, 9:57:21 AM10/14/11
to Ruby on Rails: Talk
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

Tim Shaffer

unread,
Oct 14, 2011, 10:19:19 AM10/14/11
to rubyonra...@googlegroups.com
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

unread,
Oct 17, 2011, 8:06:37 AM10/17/11
to Ruby on Rails: Talk


@Tim thanks for your help, your idea works fine

thanks,
-pab

fahim patel

unread,
Oct 6, 2012, 8:27:32 AM10/6/12
to rubyonra...@googlegroups.com
Pab wrote in post #1027023:
> @Tim thanks for your help, your idea works fine
>
> thanks,
> -pab

Thanks u all

Regards

Fahim Babar Patel

--
Posted via http://www.ruby-forum.com/.
Reply all
Reply to author
Forward
0 new messages