Chapter 6 (continued)
Ok. Back to work.
Page 61. I need to add another column to my table. I’m going to do something similar to what the book does: alter the script I have that creates the table, although KirbyBase does have an #add_column method that would work also.
So, I open up db/development/create_db.rb. I need to add a column called “date_available”. The column type is datetime. No problem, KirbyBase has a DateTime column type. Here’s what the modified line in the script now looks like:
db.create_table(:products, :title, :String,
:description, :String, :image_url, :String,
:price, :Float, :date_available, :DateTime)
Now, I just execute the create_db.rb script again to recreate the table.
Page 62. I run the command to regenerate the Admin scaffolding.
Page 63. I refresh the browser and find myself back at the “Listing Products” screen. I click on “New product”. I notice that the “Date Available” field shows up a the bottom of the screen. So far so good. I enter a new record, leaving the default date of today, and click “Create”. Uh, oh. Another full-screen error. It is telling me that there is invalid data in the “date_available” field. The trace goes back to the #validate_input method in KirbyBase. Obviously, the data that Rails is passing back to KirbyBase is not a DateTime. I throw a couple of debugging “puts” in kirbybase.rb and rerun. It turns out that Rails is trying to save a Time object in the date_available field, and, since KirbyBase has it defined as a DateTime, it throws up.
Well, I will need to send this off to Assaph to see if Rails is supposed to be interpreting a KirbyBase DateTime field type as a Time field type. In the meantime, a quick fix is to simply change the field type in my KirbyBase table to Time. It will work just as well for this exercise. So, I open up create_db.rb again and change DateTime to Time. Re-execute create_db.rb. Rerun the Admin scaffolding. Click “New product” again. Oops! Where’s the “Date Available” field? The “Date Available” label shows on the form, but there is no form field. This looks like another issue. It appears that Rails does not know how to render an input field for a KirbyBase :Time field. So, I send this info off to Assaph in an email.
Now, how do I temporarily work around this issue so I can keep going. I’ve got it. First of all, I change the “Date Available” field type in my table back to DateTime. Next I regenerate the scaffolding. This will make sure that Rails creates a form field for “Date Available”. But, Rails is going to pass back a Time field to KirbyBase and KirbyBase is expecting a DateTime. Ok, I open up my KirbyBase table in a text editor and change the field type for “Date Available” back to :Time. Now, I make sure not to regenerate the scaffolding. Now, go back to the “Listing Products” screen. Click on “New product”. Fill in the info. Click on “Create”. Bingo! It works. My new record is staring me in the face, including the “Date Available” data.
Whew! I’m tired again. Blogging is tough work. I’m going to bed.