Topic: Does every table in my database need to have a controller?

Does every table in my database need to have a controller?

I am creating a small address book app and have a table for contacts and another for their addresses. When I make an edit form for an address that uses a definition in the Contacts controller, the data isn't pre-populated. However, when I do the same thing, but put that definition in the Address controller, it is pre-populated. Why is that? Does it make a difference which controller I use?

Thanks so much for any help,
Andrew

Re: Does every table in my database need to have a controller?

Controllers don't have any direct relationship to tables or models. There's no magic going on. You can name the controller anything you want, but it is convention to give it the plural form of the primary model the controller manages.

I'm guessing the data isn't prepopulated because you aren't creating the @address instance variable in the controller's edit action. The form helpers will use this instance variable to populate the fields.

# in edit action
@address = Address.find(params[:id])

# or maybe this, depending upon how you do it
@address = @contact.address

Railscasts - Free Ruby on Rails Screencasts

Re: Does every table in my database need to have a controller?

Thanks so much Ryan, that clears things up a bit. It turns out that I was creating the @address instance variable but it wasn't recognizing it because I had misplaced the "end" statement. But now I know that I am free to create controllers on whim wink Thank you.