Philip Rhoades
2006-Jan-05 04:42 UTC
[Rails] recipes/categories to books/authors but listing doesn''t work
People, I have gone through the cookbook example: http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=1 and everything worked fine an a FC4/Postgres setup. I then tried to reproduce this success with something that would be useful to me - a library db using books & authors tables. The author list works but NOT the book list (unless I take line #17 out). I get this message: Showing app/views/book/list.rhtml where line #17 raised: You have a nil object when you didn''t expect it! The error occured while evaluating nil.last_name Extracted source (around line #17): 14: <% @books.each do |book| %> 15: <tr> 16: <td><%= link_to book.title, :action => "show", :id => book.id %></td> 17: <td><%= book.author.last_name %></td> 18: </tr> 19: <% end %> 20: </table> but as far as I can tell I have done the right things in the controllers, models and views: author_controller.rb: @authors = Author.find :all, :order => ''last_name, first_name'' book_controller.rb: @books = Book.find :all, :order => ''title, back'' author.rb: has_many :books book.rb: belongs_to :author book/list.rhtml: <table border="0"> <tr> <td width="60%"><p align="left"><i><b>Book</b></i></td> <td width="20%"><p align="left"><i><b>Author</b></i></td> </tr> <% @books.each do |book| %> <tr> <td><%= link_to book.title, :action => "show", :id => book.id %></td> <td><%= book.author.last_name %></td> </tr> <% end %> </table> authors table: id | integer | not null default nextval(''public.authors_id_seq''::text) last_name | character varying(20) | first_name | character varying(20) | other_name | character varying(20) | books table: id | integer | not null default nextval(''public.books_id_seq''::text) media | character(2) | not null title | character varying(50) | not null year | integer | not null cat | character(1) | not null back | character(1) | own | character(1) | author_id | integer I have been battling with this for ages but can''t see any bugs left - any suggestions? Thanks, Phil. -- Philip Rhoades Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275) GPO Box 3411 Sydney NSW 2001 Australia Mobile: +61:(0)411-185-652 Fax: +61:(0)2-8221-9599 E-mail: phil@pricom.com.au
Damon Clinkscales
2006-Jan-05 06:30 UTC
[Rails] Re: recipes/categories to books/authors but listing doesn''t work
Philip Rhoades <phil@...> writes:> I have been battling with this for ages but can''t see any bugs left - > any suggestions?It would appear that you have a book with no author. replace the stuff in your view with <%=book.inspect%> and see if any book has a nil author. -damon http://damonclinkscales.com/
Possibly Parallel Threads
- Modifying script generated scaffold output Books/Authors
- categories/recipes & books/descriptions - has_many vs has_one => id question
- Cookbook recipes eg - ordering categories in the recipe pull-down box
- Scaffolding - disabling Create, Edit, Destroy for some user logins?
- Repost: Pluralization of non-noun names