Forum: Ruby on Rails recipes/categories to books/authors but listing doesn't work

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Philip R. (Guest)
on 2006-01-05 06:43
(Received via mailing list)
People,

I have gone through the cookbook example:

http://www.onlamp.com/pub/a/onlamp/2005/01/20/rail...

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 R.

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:  removed_email_address@domain.invalid
Damon C. (Guest)
on 2006-01-05 08:32
(Received via mailing list)
Philip R. <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/
This topic is locked and can not be replied to.