Chapter 6
Ok, here we go. As I said in my previous entry, I’m working through chapters 6 through 11 of the book, Agile Web Development with Rails, building the Depot application using Ackbar/KirbyBase instead of MySQL.
Alright, page 53. I change to my work directory and type:
rails depot
And Rails creates the application.
Page 54. Instead of logging into MySQL and creating the development, test, and production databases, all I need to do is create three directories below the db directory, since KirbyBase considers a directory as a database. To create the products table, I will write a Ruby script to create it in KirbyBase. Here’s what that script looks like:
require 'kirbybase'db = KirbyBase.newdb.drop_table(:products) if db.table_exists?(:products)db.create_table(:products, :title, :String,:description, :String, :image_url, :String,:price, :Float)
I will place this script in the db/development directory and execute it to create the table.
Pages 55-56. Next, I’m going to edit the config/database.yml file to tell it to use KirbyBase:
development:adapter: kirbybasedatabase: db/developmenttest:adapter: kirbybasedatabase: db/testproduction:adapter: kirbybasedatabase: db/production
I have to do one more thing to get Rails to use KirbyBase. I need to edit config/environment.rb and add
require 'kirbybase_adapter'
to the top of the file.
Page 57. Generate the scaffold for the Admin function:
ruby script/generate scaffold Product Admin
Now, start the server:
ruby script/server
I switch over to Firefox and go to localhost:3000/admin and, BAM my first error. The error message tells me that there is a NoMethodError in Admin#index. Further, it says, “You have a nil object when you didn’t expect it!”. The trace points to line 649 in kirbybase_adapter.rb, in method #translate_sql_to_code.
I end up backtracking through three or four methods and I find the problem in the overridden count method (line 656 in kirbybase_adapter.rb). It is expecting an empty args array if we want to count all records, but Rails is actually passing in an args array of [nil, nil]. So, I modified the line to look like this:
if args.empty? or args == [nil, nil]
I restart the server, click reload on my browser, and up pops the “Listing Products” page!
I click on “New product”, fill in the info, and click on “Create”. The browser goes back to the “Listing products” screen with my newly entered record showing up. Ackbar works!
Ok, I think that’s enough for tonight. Hopefully I’ll get through the rest of Chapter 6 in my next post.
No Comments Yet
Be the first to comment!