Hey Kevin,

Make sure your Models have the appropriate relationships:

Authors Model
has_many :books

Books Models
belongs_to :author

In your controller you'll need to find all the authors:  This will give you the information you need to populate your select fields.

@authors = Author.find(:all)

Then, in your views use something like this:
<%= f.select :author_id, @authors.collect { |a| [a.name,a.id] } %>

This Model/View/Controller concept is pretty key to Rails. If you haven't yet had a chance to go through a handful of tutorials, I highly recommend doing that first.

A few good starters:

http://www.rubyonrails.org/docs

Good luck,

-Nathan


Kevin Burk wrote:
Okay, here's yet another newbie question.

I'm creating an application with several linked tables-for example, Authors and Books. The "Book" model has its own primary ID (automatically generated), plus the following fields:

author_id
title
subtitle
subject

The author_id field is a foreign key linking it to the Authors table.

I want to create a static scaffold form so that I can generate new books. The "author_id" field does not show up in the scaffold, and it won't accept the field.

ULTIMATELY, I want this field to be a pull-down menu that allows me to select an author name and it goes back to a hash in the book model to pull the values (and automatically inserts the correct author_id).

I've tried creating just a plain text field in the static scaffold form:

<p><label for="">Name</label><br/>
<%= text_field 'book', 'author_id'  %></p>

I've also tried form.select and various hashes, but the issue seems to be that I can't access the field (Book.author_id) from the scaffold.

What gives?

Thanks,

Kevin Burk



_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby

Reply via email to