# undefined method + method(argument)

**URL:** <https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134>\
**Category:** rubyonrails-talk\
**Created:** [October 16, 2010, 2:46pm UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134 "2010-10-16T14:46:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 16, 2010, 2:46pm UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/1 "2010-10-16T14:46:49Z")

</div>

Hi.. I would appreciate some help with the following, because I have tried to find the solution on the web, but I am still no closer to resolving my issue..

I have a store\_controller with an index method and a view for index..

I have also created a new method called find\_first\_in\_group. This method does not have a view.. I want to call it from the index view.. Also, this method needs an argument to look up a specific field.

In other programming languages it would be something like: find\_first\_in\_group(image\_url). When I try this it says 'undefined method'. How to tell it where to find the method?

---

<div class="post-metadata">

**Author:** ![Luke\_Cowell](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Luke\_Cowell](https://discuss.rubyonrails.org/u/Luke_Cowell)\
**Post date:** [October 16, 2010, 3:11pm UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/2 "2010-10-16T15:11:13Z")

</div>

Where are you calling find\_first\_in\_group and what is it supposed to do.

Luke

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [October 16, 2010, 3:30pm UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/3 "2010-10-16T15:30:58Z")

</div>

Can you show the code where you are calling it, with some context around it, and the code of the method please? Also which files they are in and the exact error message (and show which line the error message is indicating).

Colin

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [October 16, 2010, 6:29pm UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/4 "2010-10-16T18:29:44Z")

</div>

> I have a store\_controller with an index method and a view for index..
> 
> I have also created a new method called find\_first\_in\_group. This method does not have a view.. I want to call it from the index view.. Also, this method needs an argument to look up a specific field.

So you've added a method to your controller, and you want to call that method from a view? If so, you need to define it as a "helper method" in the controller:

&nbsp;&nbsp;helper\_method :find\_first\_in\_group

BUT... without a bit more information, it's not possible to say for sure, but it's possible that it would be more appropriate to put the method in a model as a class method. But hard to say with what you've given us.

> In other programming languages it would be something like: find\_first\_in\_group(image\_url).

In Ruby it's exactly the same - although it's common to drop the parentheses 😉

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 16, 2010, 10:32pm UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/5 "2010-10-16T22:32:32Z")

</div>

Thanks guys.. Here is some further information to help you determine what the problem is..

\*\*\*\*\*\*\*\*\*\*\*\*\*Index View(Store)\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<% if @cameras.count \> 1 then %\> &nbsp;&nbsp;\<% @cameratypes.each do |camera| -%\> &nbsp;&nbsp;\<% @image\_url = find\_first\_in\_group(camera.camtype)%\> &nbsp;&nbsp;\<%= link\_to(image\_tag(@image\_url)) %\> &nbsp;&nbsp;\<div\> &nbsp;&nbsp;&nbsp;&nbsp;\<h3\>\<%=h camera.camtype%\>\</h3\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<% end %\> \<% end %\>

\*\*\*\*\*\*\*\*\*\*\*\*\*Store\_controller\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

class StoreController \< ApplicationController &nbsp;&nbsp;def index &nbsp;&nbsp;&nbsp;&nbsp;@cameras = Camera.find\_cameras\_for\_sale &nbsp;&nbsp;&nbsp;&nbsp;@cameratypes = Camera.find\_cameras\_by\_camtype &nbsp;&nbsp;&nbsp;&nbsp;@cameramakes = Camera.find\_cameras\_by\_make &nbsp;&nbsp;end

##This is only suppose to pass the value on to the main camera model &nbsp;&nbsp;def find\_first\_in\_group(camtype) &nbsp;&nbsp;&nbsp;&nbsp;Camera.find\_first\_group\_image(camtype) &nbsp;&nbsp;end end

\*\*\*\*\*\*\*\*\*\*\*\*\*Camera model\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ####Just an extract of the specific method being used

&nbsp;&nbsp;def self.find\_first\_group\_image(camtype) &nbsp;&nbsp;&nbsp;&nbsp;find(:first, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select =\> "image\_url", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:conditions =\> ["camtype = ?", camtype]) &nbsp;&nbsp;end

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Error message\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

NoMethodError in Store#index

Showing app/views/store/index.html.erb where line #5 raised:

undefined method `find\_first\_in\_group' for #\<ActionView::Base:0x7f6bef401980\>

Extracted source (around line #5):

2: 3: \<% if @cameras.count \> 1 then %\> 4: \<% @cameratypes.each do |camera| -%\> 5: \<% @image\_url = find\_first\_in\_group(camera.camtype)%\> 6: \<%= link\_to(image\_tag(@image\_url)) %\> 7: \<div\> 8: \<h3\>\<%=h camera.camtype%\>\</h3\>

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 17, 2010, 2:20am UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/6 "2010-10-17T02:20:51Z")

</div>

Can someone, please, please, pretty please, with sugar and icing on top, get back to me on this? I know it is going to be something simple.. Don't mean to be pushy, but I've got a deadline to meet today.. And I am pulling my hair out.. ☹

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 17, 2010, 3:56am UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/7 "2010-10-17T03:56:06Z")

</div>

Michael Pavling wrote in post #954819:

> On 16 October 2010 15:46, Evanoshki Brataslavainskinski

> So you've added a method to your controller, and you want to call that method from a view? If so, you need to define it as a "helper method" in the controller:
> 
> &nbsp;&nbsp;helper\_method :find\_first\_in\_group
> 
> BUT... without a bit more information, it's not possible to say for sure, but it's possible that it would be more appropriate to put the method in a model as a class method. But hard to say with what you've given us.
> 
> > In other programming languages it would be something like: find\_first\_in\_group(image\_url).
> 
> In Ruby it's exactly the same - although it's common to drop the parentheses 😉

Thanks to Michael Pavling for guiding me in the right direction

---

<div class="post-metadata">

**Author:** ![Luke\_Cowell](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Luke\_Cowell](https://discuss.rubyonrails.org/u/Luke_Cowell)\
**Post date:** [October 17, 2010, 4:00am UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/8 "2010-10-17T04:00:43Z")

</div>

Did you try Michael's suggestion of adding this to your controller?

helper\_method :find\_first\_in\_group

This probably isn't the best approach, but you could change line 5 to something like this: \<% @image\_url = Camera.find\_first\_group\_image(camera.camtype)%\>

HTH

Luke

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 17, 2010, 5:17am UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/9 "2010-10-17T05:17:10Z")

</div>

Luke Cowell wrote in post #954878:

> Did you try Michael's suggestion of adding this to your controller?
> 
> helper\_method :find\_first\_in\_group
> 
> This probably isn't the best approach, but you could change line 5 to something like this: \<% @image\_url = Camera.find\_first\_group\_image(camera.camtype)%\>
> 
> HTH
> 
> Luke

Yes thanks Luke.. That was what I was looking for..

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [October 17, 2010, 8:36am UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/10 "2010-10-17T08:36:10Z")

</div>

> Thanks guys.. Here is some further information to help you determine what the problem is..

You do seem to be jumping around the houses a little to get the record you're after... you have a controller method that calls a class method on a model, which you make a helper\_method to use in a view, which you access while looping through instances of the model that the class method is attached to...

I would simplify it a little (although there are probably better ways to do it by association... are "CameraTypes" a model?): anyway... make the model's class method an instance method, and access it from each instance of the camera object.

> \*\*\*\*\*\*\*\*\*\*\*\*\*Camera model\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* def find\_first\_group\_image find(:first, :select =\> "image\_url", :conditions =\> ["camtype = ?", self.camtype]) end
> 
> \*\*\*\*\*\*\*\*\*\*\*\*\*Index View(Store)\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> \<% if @cameras.count \> 1 then %\> \<% @cameratypes.each do |camera| -%\> \<% @image\_url = camera.find\_first\_in\_group %\> \<%= link\_to(image\_tag(@image\_url)) %\> \<div\> \<h3\>\<%=h camera.camtype%\>\</h3\> \</div\> \<% end %\> \<% end %\>

PS All of the "find cameras by xxx" methods... are you using named\_scopes for them? (judging by the names, I think possibly you aren't, and you probably should 🙂

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [October 17, 2010, 8:36am UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/11 "2010-10-17T08:36:54Z")

</div>

It is generally not considered good practice to access model methods directly from the view. Possibly a better way would be to define an instance method, image\_url, in the Camera class. Then you could say \<%= link\_to(image\_tag(camera.image\_url)) %\>

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 18, 2010, 3:37pm UTC](https://discuss.rubyonrails.org/t/undefined-method-method-argument/55134/12 "2010-10-18T15:37:42Z")

</div>

Colin Law wrote in post #954895:

> > Luke Cowell wrote in post #954878:
> > 
> > > Did you try Michael's suggestion of adding this to your controller?
> > > 
> > > helper\_method :find\_first\_in\_group
> > > 
> > > This probably isn't the best approach, but you could change line 5 to something like this: \<% @image\_url = Camera.find\_first\_group\_image(camera.camtype)%\>
> 
> It is generally not considered good practice to access model methods directly from the view. Possibly a better way would be to define an instance method, image\_url, in the Camera class. Then you could say \<%= link\_to(image\_tag(camera.image\_url)) %\>

Right. Colin is being nice, so I'll be less nice: the view should NEVER EVER EVER touch the database.

I am beginning to think that, for this reason and others, template languages (such as Mustache) where you can't call \*any\* model methods are the way to go...

> Colin

Best,
