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? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Where are you calling find_first_in_group and what is it supposed to do. Luke On 2010-10-16, at 7:46 AM, Evanoshki Brataslavainskinski wrote:> 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? > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 October 2010 15:46, Evanoshki Brataslavainskinski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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?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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 October 2010 15:46, Evanoshki Brataslavainskinski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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: 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 ;-) -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Evanoshki Brataslavainskinski
2010-Oct-16 22:32 UTC
Re: undefined method + method(argument)
Thanks guys.. Here is some further information to help you determine what the problem is.. *************Index View(Store)******************* <% if @cameras.count > 1 then %> <% @cameratypes.each do |camera| -%> <% @image_url = find_first_in_group(camera.camtype)%> <%= link_to(image_tag(@image_url)) %> <div> <h3><%=h camera.camtype%></h3> </div> <% end %> <% end %> *************Store_controller******************* class StoreController < ApplicationController def index @cameras = Camera.find_cameras_for_sale @cameratypes = Camera.find_cameras_by_camtype @cameramakes = Camera.find_cameras_by_make end ##This is only suppose to pass the value on to the main camera model def find_first_in_group(camtype) Camera.find_first_group_image(camtype) end end *************Camera model*********************** ####Just an extract of the specific method being used def self.find_first_group_image(camtype) find(:first, :select => "image_url", :conditions => ["camtype = ?", camtype]) 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> ************************************************ The code is supposed to step through all the different camera types and then look up the first table entry for every type. It should then just extract the image_url value of that first entry of the unique camera type. I then wish to turn this into clickable links which will display the camera models of every type. I have managed to get this working by doing the SQL(find) from the index view, but will be penalized for my assignment if I leave it like that, because we are told to do all processing in the model itself. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Evanoshki Brataslavainskinski
2010-Oct-17 02:20 UTC
Re: undefined method + method(argument)
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.. :-( -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Evanoshki Brataslavainskinski
2010-Oct-17 03:56 UTC
Re: undefined method + method(argument)
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: > > 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 -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On 2010-10-16, at 7:20 PM, Evanoshki Brataslavainskinski wrote:> 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.. :-( > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Evanoshki Brataslavainskinski
2010-Oct-17 05:17 UTC
Re: Re: undefined method + method(argument)
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 > > LukeYes thanks Luke.. That was what I was looking for.. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 October 2010 23:32, Evanoshki Brataslavainskinski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 :-)> @cameras = Camera.find_cameras_for_sale > @cameratypes = Camera.find_cameras_by_camtype > @cameramakes = Camera.find_cameras_by_make-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 October 2010 06:17, Evanoshki Brataslavainskinski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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)) %> Colin -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #954895:> On 17 October 2010 06:17, Evanoshki Brataslavainskinski > <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> 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...> > ColinBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.