I''m just starting with Ruby on Rails, and I am attempting to customize my first list view. I have a table named ''photos_films'', a model constructed in the usual way, and a controller that looks like: class PhotosFilmController < ApplicationController scaffold :photos_film def list end end The template ''list.rhtml'' looks like: <html> <head> <title>Films</title> </head> <body> <h1>Photos :: Films</h1> <table class="list"> <tr> <th>Code</th> <th>Title</th> <th>Places</th> <th>Start</th> <th>End</th> </tr> <% @photos_films.each do |film| %> <tr> <td><%= link_to film.code, :action => "show", :id => film.id %></td> <td><%= film.title %></td> <td><%= film.places %></td> <td><%= film.startdate %></td> <td><%= film.enddate %></td> </tr> <% end %> </table> <p><%= link_to "New", :action => "new" %></p> </body> </html> When I try to access the page, Rails throws an error at the line with the ''each'' in it, reporting: WARNING: You have a nil object when you probably didn''t expect it! Odds are you want an instance of Array instead. If I just use the scaffold without any attempt to define my own ''list'' method and template, everything works fine. My guess is that ''@photos_film'' is not the correct name for the array of items in the table. My question is, is there a way to find out what name I _should_ be using for this? On the off-chance that this is actually caused by something other than my own lack of knowledge of Ruby and Rails, I should add that this is the current release of Rails running under Ruby 1.8.2 on MacOS X, Apache with FastCGI, and MySQL 4.19. Thanks in advance for any suggestions, Angus -- "I am here by the will of the people ... and I *will* ["Metrophage", not leave until I get my raincoat back." Richard Kadrey]
Your list function in the controller (which is presently empty) needs to put something in @photo_films. The default (provided by scaffold) does something like "@photos_films = PhotosFilm.find(:all)" but you are overriding it. That''s fine, and you''re free to do whatever you want in that function, and call it whatever you want, but whatever you decide to call it in the view it has to be initialized (by the same name) in the controller. If you just want the default (find all) behavour you could just get rid of the "def list/end" part, or add the "find". If you are wanting to be more selective (find only some) see the ActiveRecord::Base documentation for fancier finds. --MarkusQ
Angus McIntyre wrote:>I''m just starting with Ruby on Rails, and I am attempting to customize >my first list view. > >I have a table named ''photos_films'', a model constructed in the usual >way, and a controller that looks like: > > class PhotosFilmController < ApplicationController > > scaffold :photos_film > > def list > end > end > >The template ''list.rhtml'' looks like: > > <html> > <head> > <title>Films</title> > </head> > <body> > > <h1>Photos :: Films</h1> > > <table class="list"> > <tr> > <th>Code</th> > <th>Title</th> > <th>Places</th> > <th>Start</th> > <th>End</th> > </tr> > > <% @photos_films.each do |film| %> > <tr> > <td><%= link_to film.code, :action => "show", :id => >film.id %></td> > <td><%= film.title %></td> > <td><%= film.places %></td> > <td><%= film.startdate %></td> > <td><%= film.enddate %></td> > </tr> > <% end %> > > </table> > > <p><%= link_to "New", :action => "new" %></p> > > </body> > </html> > >When I try to access the page, Rails throws an error at the line with >the ''each'' in it, reporting: > > WARNING: You have a nil object when you probably didn''t > expect it! Odds are you want an instance of Array instead. > >If I just use the scaffold without any attempt to define my own ''list'' >method and template, everything works fine. > >My guess is that ''@photos_film'' is not the correct name for the array of >items in the table. My question is, is there a way to find out what name >I _should_ be using for this? > >The scaffolding does magic that creates a list method and sets @photos_film. Since you have defined a list method (and overrode the scaffolding one) you''ll need to set @photos_film, something like: def list @photos_films = PhotosFilm.find(:all); end
Norman Timmler
2005-Aug-29 07:15 UTC
Re: ''You have a nil object'' in (very) basic template
Am Sonntag, den 28.08.2005, 08:14 -0400 schrieb Angus McIntyre:> class PhotosFilmController < ApplicationController > > scaffold :photos_film > > def list > end > endif you define you own list method, you have to put the logic into it, that would be otherwise defined through the scaffold mechanism. class PhotosFilmController < ApplicationController scaffold :photos_film def list @photos_film = PhotosFilm.find :all end end you could access only instance variables defined in your controller in the view. ciao, norman
Hi Angus, On 28.8.2005, at 15.14, Angus McIntyre wrote:> I''m just starting with Ruby on Rails, and I am attempting to customize > my first list view. > > I have a table named ''photos_films'', a model constructed in the usual > way, and a controller that looks like: > > class PhotosFilmController < ApplicationController > > scaffold :photos_film > > def list > end > end > > > When I try to access the page, Rails throws an error at the line with > the ''each'' in it, reporting: > > WARNING: You have a nil object when you probably didn''t > expect it! Odds are you want an instance of Array instead. > > If I just use the scaffold without any attempt to define my own ''list'' > method and template, everything works fine. > > My guess is that ''@photos_film'' is not the correct name for the > array of > items in the table. My question is, is there a way to find out what > name > I _should_ be using for this?You don''t get the contents of the table automatically. You have to fetch the data from the database first. Put this inside your list method in the controller to populate the variable you''re using in the view: @photos_film = PhotosFilm.find_all One more point: Are you sure you don''t want to have two models, Photo and Film, that have some kind of association between each other? Having a model like yours as the basis of your app sounds a bit odd to me. //jarkko> On the off-chance that this is actually caused by something other than > my own lack of knowledge of Ruby and Rails, I should add that this is > the current release of Rails running under Ruby 1.8.2 on MacOS X, > Apache > with FastCGI, and MySQL 4.19. > > Thanks in advance for any suggestions, > > Angus > > -- > "I am here by the will of the people ... and I *will* > ["Metrophage", > not leave until I get my raincoat back." > Richard Kadrey] > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 28 Aug 2005, at 1:14 pm, Angus McIntyre wrote:> I have a table named ''photos_films'', a model constructed in the usual > way, and a controller that looks like: > > class PhotosFilmController < ApplicationController > > scaffold :photos_film > > def list > end > end > > The template ''list.rhtml'' looks like:<snip>> <% @photos_films.each do |film| %><snip>> > When I try to access the page, Rails throws an error at the line with > the ''each'' in it, reporting: > > WARNING: You have a nil object when you probably didn''t > expect it! Odds are you want an instance of Array instead.Your controller method needs to create all the objects that you want to use in your view. So in your ''list'' method you need something like @photos_film = PhotosFilm.find_all The point is that there is no special connection between a model called PhotosFilm and a controller called PhotosFilm. So don''t expect any objects to be automagically created for you. Chris
Angus McIntyre
2005-Aug-29 12:47 UTC
Re: ''You have a nil object'' in (very) basic template
In article <angus-6A0CB6.08142328082005-WnDAp88bSu3NLxjTenLetw@public.gmane.org>, Angus McIntyre <angus-e+AXbWqSrlAAvxtiuMwx3w@public.gmane.org> wrote:> I''m just starting with Ruby on Rails, and I am attempting to customize > my first list view.... and failed to read the tutorial correctly (or completely), so that I left off a crucial line in the controller. The list definition should have read: def list @photos_films = PhotosFilm.find_all end Apologies for taking bandwidth with boneheadedness, thanks to all who would have written. Angus -- "I am here by the will of the people ... and I *will* ["Metrophage", not leave until I get my raincoat back." Richard Kadrey]
Your list function in the controller (which is presently empty) needs to put something in @photo_films. The default (provided by scaffold) does something like "@photos_films = PhotosFilm.find(:all)" but you are overriding it. That''s fine, and you''re free to do whatever you want in that function, and call it whatever you want, but whatever you decide to call it in the view it has to be initialized (by the same name) in the controller. If you just want the default (find all) behavour you could just get rid of the "def list/end" part, or add the "find". If you are wanting to be more selective (find only some) see the ActiveRecord::Base documentation for fancier finds. --MarkusQ