Enough with concepts, ideas and diagrams: it's time to start coding something. Everyone knows what's the first step when creating a Rails applications, but anyhow, here it is:
/bin/sh: chroma: not found
Then I create a new development database, load it up with the schema I previously prepared and modify the
config/database.yml to be able to connect to it. Nothing new here.
I actually had to modify the schema a little bit:
- I changed all the names for the foreign keys to something more evocative than “has_many” or “has_one”
- I added a level column to the states, availabilities and conditions table
- I removed the description column from the categories table
Great, but… hang on: now some of the database tables look awfully similar with each other:
- statuses
- states
- roles
- types
- tags
- conditions
- availabilities
- categories
They all have a name column, some of them have a name column as well, they'll hold only a relative small number
of records which will hardly ever be deleted. In fact, I was tempted to use Enums for some of those
things…
Anyhow, I'll still have to add and modify data in those tables, so it looks like I kinda need to create 8
controllers, 8 models and about four views for each one of them. No way. Fair enough for the controllers and models,
but I'm not going to create 32 views which all look exactly the same. Rails should be smarter than that!And it
is, luckily. Derek Sivers & C. came out with an interesting Shared Controller concept, which could be just
what I'm looking for in this case. Actually I need something really simple in this case:
- Put all the CRUD logic into one controller
- Create only one set of views
Here's the controller:
app/controllers/admin/lite_controller.rb
/bin/sh: chroma: not foundThen all I need to do is create eight controllers with just a few lines of code in each:
app/controllers/admin/statuses_controller.rb
/bin/sh: chroma: not foundBasically, I just need to specify which model the specific controller takes care of, Ruby's inheritance does the rest. The model name will be passed to the views like this:
app/controllers/admin/lite_controller.rb
/bin/sh: chroma: not foundAnd each method uses the
modelmethod to access the model, like this:app/controllers/admin/lite_controller.rb
/bin/sh: chroma: not foundNote how the params are collected:
/bin/sh: chroma: not found
params[:"#{item_name.downcase}"]@ at runtime becomesparams[:status]orparams[:role]etc. etc., depending on which controller is called. Sweet.The views? Modified accordingly:
app/views/lite/edit.rb
/bin/sh: chroma: not foundapp/views/lite/_form.rb
/bin/sh: chroma: not found