Not sure if I'm reading this right, but it seems like Scaffold will not do a one-to-many relationship in its entirety. For instance, if I create messages
with scaffold and then I want comments
on those messages
(one message
-> many comments
), I have to go through and change everything. For instance, I have to change this in the comment
's new
view
<% form_for(@comment) do |f| %>
to this
<% form_for([@message, @comment]) do |f| %>
and then change the Action to set up the @message
var... amongst other things.
This cannot currently be done automatically with Scaffold, right?
Yes. Scaffold works for a model and related controller. It does not take care of or work with relationships.
Scaffold's primary objective is to get CRUD going on a model using a controller and related views. That's all. Any other requirement like relationships has to be coded manually.
You don't need a heavy rails admin framework to get one-to-many relationships working.
You can use scaffolding to get most of the way there.
A little more work in the controller and _form view will get you the rest of the way there.
Here's how...
Assuming we have a beers table:
And a developers table that has a foreign key (beer_id) referencing the beers table:
We can use scaffolding to create both tables:
The scaffold command creates the controllers and views for each model.
We will need to modify our controllers and views a little bit to get the dropdown menu to select a beer for each developer:
app/views/developers/_form.html.erb
Replace the generated text_field and label for beer_id with the following:
app/controllers/developer_controller.rb
Edit the controller's new and edit methods:
Screen Shots
Notes
Rails scaffolding is great. Look at all the files that it creates for you:
All you have to do is know which files to modify when you want more than basic CRUD operations.
Hope that helps. ~ Lex
This is true, but, it's not the end of the story. There are at least two alternatives to Scaffold that both work quite well and automatically pick up on relationships between classes (based on your ActiveRecord relationship indicators like has_many). One of these alternatives is Streamlined and the other is ActiveScaffold.
They're mainly helpful for entering in data that your system requires that is not user entered data. For example, I use them for administrative tasks on tables where there's no point in building a complete UI for CRUD when one of the scaffold alternatives will do the job just fine for a seldom used feature. You wouldn't want to use them for comments on messages though.
Scaffolds are scaffolds. When you want anything other than a CRUD on a table (which is what a scaffold is/does), you need to alter the generated scaffolding code, or roll your own.
Note that there are projects like Hobo for Rails which allow you to keep your fields and associations within the model itself. You can't scaffold associations, but it's pretty close.
You end up paying for this sugar by having a lot more of the application built behind your back. Instead of rolling your own, you're usually subtracting out what you need from a large bank of prebuilt behaviors.