Ruby on Rails (2)

Continued from Part One

Accessing Rails

Well let's start with the happy news:

Ruby on Rails Startup Screen Shot

Yes folks, that's the start-up screen for Ruby on Rails. It's displaying on my laptop at 11:30 in the morning, Saturday, October 15, six days after I started my installation.

So what happened? The work that I did last Monday to fix the Rails install was in fact successful. However, well, I'll let Luc, our network admin, explain it:

   Ok, I'm not familiar at all with this software; I ran the webrick server and it
   is working; the only caveat to this is, because it's running on port 3000,
   the firewall (NRC's firewall) will not allow traffic through.  So, on VPN it
   should be fine, that is if www.downes.ca resolves to 10.1.3.226 and not the
   external IP.

   As far as configuring it in Apache, from what I read no additional setup is
   required, since the ruby interpreter is specified with a shebang line, it behaves
   like any cgi script would; so I added a handler for .rb files.  The lines you
   added are for modrewrite, which aren't compiled into this new apache I compiled
   the other day, if you need it I can add it... but basically what modrewrite
   does is allow by means of special rules in .htaccess files or in the main
   httpd.conf, to manipulate the URL.  For example, if your search page is search.cgi,
   but you'd want someone to type in http://www.downes.ca/search/hampsters...
   you could use modrewrite so that url would be treated as
   http://www.downes.ca/search.cgi?q=hampsters ...  also note that badily written
   rules can really mess up a server..

The handler for the .rb files would be the extra lines of code to add to httpd.conf, as mentioned in Step 5 in Getting Started With Rails (I love the way the document simply refers to "your Apache config file" without ever telling you what the filename is. We still haven't added mod_rewrite to the Apache server (which means my install of ELGG is also crapping out in some other directory - but it was just a test install and not production, so I don't need to worry about that.

The other part, in the first paragraph of Luc's response, was as I had feared: our network configuration is blocking access to port 3000. This is actually pretty common and not something I disagree with; my site, like so many other sites, is constantly subject to attacks and probes, so blocking unused ports is a wise precaution. But why wasn't it working, even when I was on VPN? Luc has another email:

   Your password expired... I think it should work for now, but you'll need to change it soon.

Whoever came up with the idea of rotating passwords every three months is a moron. Seriously. I mean, even if you're going to rotate passwords at all (which I think is a bad idea, because I now have yellow sticky-notes all over the place with the latest password on it), why would you set the interval in months? I mean, this just guarantees that your password will eventually expire in the middle of a long weekend. Set the password on a Tuesday, and have it expire in, say, 12 weeks, so it will expire on a Tuesday - that is, a day when you're in the office, can actually change your password, or get help when it (inevitably) fails. And when it fails (or preferably, a few days before it fails), send an error message. Sheesh.

So anyhow, these problems addressed, Ruby on Rails is now loading. Inside the vpn only. Rails is working fine on the desktop, which is connected to the vpn, but fails on the laptop. Start up the vpn on the laptop, and it works fine. So the gateway to the outside world still isn't open. I'll send Luc a note and we'll get that fixed in a few days. (For those who are wondering - 'vpn' stands for 'virtual private network'. It enables you to create networks using the Internet using encryption and other security mechanisms to ensure that only authorized users can access the network and that the data cannot be intercepted - more).

What's the lesson here? Simply this: if you are working in anything like an institutional environment (government, university, corporation) it is very likely systems have been put into place to ensure that you do not do something like install Rails and make it available to the world. Even if you get Rails working, you have to navigate these systems. Which means, really, your best bet is to have computer services or your IT department install it for you. Unless you have access to and can manage your own firewall, you are going to run into these problems.

AddressBook

12:00 pm. I'm returning to the AddressBook guide. I am now down to the section titled 'Creating a little Content' - Listing 6. Here's what it says:

   [~/Sites/AddressBook]$ ruby script/generate model contact
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/contact.rb
      create  test/unit/contact_test.rb
      create  test/fixtures/contacts.yml
   [~/Sites/AddressBook]$ ruby script/generate controller contact
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/contact
      exists  test/functional/
      create  app/controllers/contact_controller.rb
      create  test/functional/contact_controller_test.rb
      create  app/helpers/contact_helper.rb

So there are two commands to run. I try the first one, 'ruby script/generate model contact':

   [downess@downes AddressBook]$ ruby script/generate model contact
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/contact.rb
   Permission denied - ./script/../config/../app/models/contact.rb

As usual, the example does not run as advertised. Why is it denying permission? Maybe I need to be root. I type 'su' and it runs as advertised. OK, let's run the second command, 'ruby script/generate controller contact'. It also runs as advertsed. Excellent. But what have I done? I'm not sure. I check the database, and no changes have been made. So I must have generated some code - that must be what 'script/generate' means. Now I have one table in my database, called 'contacts'. And the guide states, "Notice here that you should use the singular contact rather than the plural contacts in the corresponding table name." So I have generated a model and a controller for the table in my database. If I had named my table 'kumquats' then I would have run 'ruby script/generate model kumquat' and 'ruby script/generate controller kumquat'.

I remember reading about this a few months ago. The idea is that the logic of the application is already in the database, so the generate code looks at the structure of the database and creates code based on that pre-existing structure. Clever.

Next step:

   [~/Sites/AddressBook]$ cat app/controllers/contact_controller.rb
   class ContactController < ApplicationController
     model :contact
     scaffold :contact
   end

The Linux 'cat' command basically transfers files from input to output - its stands for 'concatenate'. I am not specifying an output file, so it is simply displaying the input ('app/controllers/contact_controller.rb') to the screen. I run the command and get:

   [root@downes AddressBook]# cat app/controllers/contact_controller.rb
   class ContactController < ApplicationController
   end

So why is my output different from the example? Ah - I have to edit the file so it looks like the one above. OK, so I'll type 'vi app/controllers/contact_controller.rb' and add the lines to match the example (see, this guide uses exactly the same format for the typing of commands and the contents of a file, calling them both 'listings' - when they are in fact very different). So I edit the file, and now when I run 'cat app/controllers/contact_controller.rb' (which I never technically needed to run) it looks like the example.

The next step involves inputting some data. The guide doesn't actually tell you how to input the data (I can imagine people opening phpMyAdmin) but I open http://www.downes.ca:3000/contact in my browser and see:

   Listing contacts
   Name 	Created on 	Updated on

   New contact

The 'new contact' line is a link, so I click it and type a name into the input field. After inputting three names like this, mine looks like the example:

   Contact was successfully created
   Listing contacts
   Name 	Created on 	Updated on
   Fred Smith 	Sat Oct 15 11:31:35 ADT 2005 	Sat Oct 15 11:31:35 ADT 2005 	Show 	Edit 	Destroy
   Grover Cleveland 	Sat Oct 15 11:31:44 ADT 2005 	Sat Oct 15 11:31:44 ADT 2005 	Show 	Edit 	Destroy
   Jeremy Presley 	Sat Oct 15 11:31:59 ADT 2005 	Sat Oct 15 11:31:59 ADT 2005 	Show 	Edit 	Destroy

   New contact

Oh I see - the instructions are simply backwards. After showing the output, we see the input screen. OK, moving on. "In order to create something a bit more custom, you need to generate a bit more code. What we need now is for Rails to explicitly write out all the scaffolding it is implicitly generating on the fly so that we can tinker with it." OK, so we'll enter the following: 'ruby script/generate scaffold Contact dependency model'. As before, we get a bunch of output. The script in this case creates some HTML pages, CSS pages, and more.

So, for example, we can change the stylesheet. The script has generated a file called 'public/stylesheets/scaffold.css'. Oh d'oh. Now I know what the 'head' command does. Just as 'tail' will display the last few lines of any file (very useful for viewing log files), 'head' will display the first few lines of a file. How many? That is specified by the number: 'head -8 public/stylesheets/scaffold.css' will show the first 8 lines of the CSS file. Why didn't I know this? Well, I would always type 'more public/stylesheets/scaffold.css', which would display the whole file for me. That has always worked just fine. OK, then:

   [root@downes AddressBook]# head -8 public/stylesheets/scaffold.css
   body { background-color: #fff; color: #333; }

   body, p, ol, ul, td {
     font-family: verdana, arial, helvetica, sans-serif;
     font-size:   13px;
     line-height: 18px;
   }

To edit this file I would open it with vi and change some of the styles. Or I could use kwrite (or Notepad on Windows) and do the same thing. OK, I get CSS so I more on. 12:45 p.m.

OK, next the instructions display a listing of 'app/controllers/contacts_controller.rb'. This file - actually some Ruby code - is a 'controller', which the guide explains, loads data into variables. Now follows some really awful explanation:

   The object Contact is the ActiveRecord object-relational mapping that the model provides.
   The variables @contacts or @contact are given data in their appropriate methods.
   The methods are themselves referred to by URLs such as http://rails.server/contacts/show/2
   (this one shows the contact with id of "2").

   The controller in this example ultimately connects to views, RHTML files that make use
   of the data values loaded into variables by the controller.

OK, unless you already have a good idea what's going on, this explanation will read like gibberish. But if you examine the code, what you see is that the controller creates functions that 'create', 'list' and 'show' records in the database, where the function and record in question are determined by the URL. So if we have a URL like 'http://rails.server/contacts/show/2' then the function is 'show' and the record to be shown is '2'. Also, note that the plural version of the table name is used - contacts, for, as the guide explains, "reasons not clear to me; we need to accept it for now" (yeah, that makes you feel really comfortable).

What the controllers actually do is connect the URl to various 'rhtml' files. An 'rhtml' file is kind of like a template, but it has bits of code in it that are executed by Ruby. For example, here is a line of code from an rhtml file:

    <%= link_to 'Show', :action => 'show', :id => contact.id %>

This code generates line in a table. It creates a link to 'show' a particular record, 'contact.id'. So the result, when executed by Ruby, will look something like this:

   Grover Cleveland

The last bit of this guide gets unfortunately brief. Basically, we can now extend our application by (a) changing the model to include additional tables, or (b) editing the database, to include additional columns. It also appears that we can change things like the rhtml files by hand.

But it's not straightforward. For example, I added a column 'age' to my contacts database. It shows up in 'show' but not at all in 'edit'. Why not, I wonder? I could make it appear by regenerating the model, probably. But it seems to me we would need to be careful here. Were we to run a command like 'ruby script/generate model contact' we could over-write all our changes (maybe not, but I can just see it happening). So it seems to me that with this set-up, it will be crucil to create back-ups of every file we code by hand. Just in case. 1:14 pm.

Four Days On Rails

OK, to be frank, I don't intend to spend four days with this document. I mean, 'Four Days' is a bit daunting, isn't it? Mind you, I'm already on my third day. Still - what attracts me to this document is a tantalizing promise near the end of day four - "Downloading a Copy of this Application." It would be great if the output of a Rails session were simply a completed application, one you wouldn't actually need Rails to run. That way, I could code something, and so long as you had Ruby, it would run. Leave the Rails installation to the professionals who are trained to deal with this kind of grief.

The 'Four Days' guide is based on a Windows application and begins with an unnecessary creation of a 'drive w:\' "to save typing". As before, we are going to create out application by running the 'rails' command. The name of our database in this instance will be 'ToDo'. Not that I need a 'ToDo' list. ;) So I run the application and get the same sort of set-up I got with AddressBook. The 'Four Days' guide helpfully explains what I got:

   app
       contains the core of the application, split between model, view,
       controller, and  helper  subdirectories
   config
       contains the database.yml file which provides details of the
       database to used with the application
   log
       application specific logs. Note: development.log keeps a trace
       of every action Rails performs - very useful for error tracking,
       but does need regular purging!
   public
       the directory available for Apache, which includes images,
       javascripts, and stylesheets subdirectories

The next step describes 'adding the application to my server' and deals with adding mod_rewrite to the httpd.conf file, as mentioned before. Should I ever create an application in Rails that I want to use I'll do this, but for now I'll simply skip the step. Similarly, 'switching to fastcgi' is something I'll skip (is this a Windows-specific thing? I've never heard of it. Why, I wonder, would 'slow cgi' ever be the default? I mean, who wants slow CGI?).

OK, now we'll check to see whether it's running. First, I run 'ps -A' to find out what the processid of the 'server' application is, and then type 'kill 3425' to shut down the server I had running for AddressBook (isn't there a nice 'shut-down' command?). Next, I'll start up the server here in the 'ToDo' directory:

   [root@downes ToDo]# script/server -d
   => Rails application started on http://0.0.0.0:3000
   [2005-10-15 12:41:23] INFO  WEBrick 1.3.1
   [2005-10-15 12:41:23] INFO  ruby 1.8.2 (2004-12-25) [i686-linux]
   [2005-10-15 12:41:23] WARN  TCPServer Error: Address already in use - bind(2)

Great. What happened? I try 'ps -A' again and see that 'server' is still running. I try killing it again, but it continues to run. What to do? It won't stop. Maybe if I 'exit' from root, and try it as my original userid. Nope, it's still running. I type 'man kill' to see if there's some way to force it. Nope, the manual is no help at all. I try a Google search and get this discussion, which simply reiterates the useless ctl/C command. Just for kicks, I try it, but it still doesn't work. Close the console window? I'm using ssh, not a console (you know, most people don't run web servers from their desktops). Exit out of ssh? Nope, it's still running. This is ridiculous - who would build a server without a 'stop' command?

I remember trying to start the server on another port. So I 'cd' back into 'ToDo' and try:

[downess@downes ToDo]$ script/server -d -p 2999
=> Rails application started on http://0.0.0.0:2999
[2005-10-15 12:53:42] INFO  WEBrick 1.3.1
[2005-10-15 12:53:42] INFO  ruby 1.8.2 (2004-12-25) [i686-linux]

This starts up OK, as expected. But can I access it? Yes!. Not outside the vpn, of course. But it gives me some room to manoevure. 2:00 p.m.

Next step. According to the 'Four days' guide, there are several versions of Rails. To see which version I'm running, I type 'gem list --local'. I get a bunch of things, including:

   [downess@downes ToDo]$ gem list --local
   *** LOCAL GEMS ***
   ...
   rails (0.13.1)
       Web-application framework with template engine, control-flow layer,
       and ORM.
   ...

Actually, this list is instructive. I see that in addition to Rails I have also installed rake, which I knew about, actionmailer, which is an email program, and several other utilities. Of course, the example in 'Four Days' uses Rails version 0.12.1. According to the guide, "I would strongly advise you to download the versions used in Four Days. This won t break anything; Ruby s gems library is designed to handle multiple versions. You can then force Rails to use the Four Days versions with the To Do List application." Uh huh. It won't break anything. Sure.

OK, so I'll install version 0.12.1. It fails. Oops, gotta be root. I type 'su' and then try again.

   [root@downes ToDo]# gem install rails --version 0.12.1
   Attempting local installation of 'rails'
   Local gem file not found: rails*.gem
   Attempting remote installation of 'rails'
   Updating Gem source index for: http://gems.rubyforge.org

loooooong pause - I check my email, then open Bloglines and catch up on some reading...

   Install required dependency activesupport? [Yn]
   Install required dependency activerecord? [Yn]
   Install required dependency actionpack? [Yn]
   Install required dependency actionmailer? [Yn]
   Install required dependency actionwebservice? [Yn]
   Successfully installed rails-0.12.1
   Successfully installed activesupport-1.0.4
   Successfully installed activerecord-1.10.1
   Successfully installed actionpack-1.8.1
   Successfully installed actionmailer-0.9.1
   Successfully installed actionwebservice-0.7.1
   Installing RDoc documentation for activesupport-1.0.4...
   Installing RDoc documentation for activerecord-1.10.1...
   Installing RDoc documentation for actionpack-1.8.1...
   Installing RDoc documentation for actionmailer-0.9.1...
   Installing RDoc documentation for actionwebservice-0.7.1...

OK, good. Now I need to tell Rails to use the newly installed packages. So I use vi and edit 'config\environment.rb' as instructed in the Four Days document. No problem, but it's a lot of picky typing - one typo and who knows what would happen?

Now we have to edit the database.yml file, just like before. So I'll create the database, 'todos', using phpMyAdmin (the Four days guide simply skips over this step). I also grant all privileges to my database userlogin. Just like before. Then I edit database.yml to access the 'todos' database. Then I execute some SQL in the database to create the table:

   CREATE TABLE `categories` (
      `id` smallint( 5 ) unsigned NOT NULL AUTO_INCREMENT ,
      `category` varchar( 20 ) NOT NULL default '',
      `created_on` timestamp( 14 ) NOT NULL ,
      `updated_on` timestamp( 14 ) NOT NULL ,
      PRIMARY KEY ( `id` ) ,
      UNIQUE KEY `category_key` ( `category` )
   ) TYPE = MYISAM COMMENT = 'List of categories'

Again, Rails requires some farly specific database definitions, and the Four Days document helpfully outlines them here. Next, it's time to create the model and controller:

   [root@downes ToDo]# script/generate model category app/models/
   /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:149:in `activate': can't activate
   activesupport (= 1.1.1), already activated activesupport-1.0.4] (Gem::Exception)

and the string of error messages follows. Uh oh.

Remember above where the author said "It won't break anything"? Well, this looks pretty broken to me. Let's try the next command, to generate the controller:

   [root@downes ToDo]# ruby script/generate controller category app/controllers/
   /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:149:in `activate': can't activate
   activesupport (= 1.1.1), already activated activesupport-1.0.4] (Gem::Exception)

Pretty much the same thing. Now the question is, is this fatal? Or will the application still run? Best thing to do is to forge ahead, I think. We'll learn pretty quickly if we have something bad here. Actually - the next step is to edit 'app\controllers\category_controller.rb', which should have been created by the 'generate model' command. So I 'cd app' to see if it's there. It's not. Similarly, there are no models in the app/models directory. The two commands have failed. It's 2:48 and once again I find myself facing some weird error.

Mostly likely, what is happening is that my current project - ToDo - is conflicting with my previous project - 'AddressBook'. While Ruby may support multiple installs of Rails, it's less clear that it supports running them at the same time. Now - that's a guess. The code itself might be borked. Or maybe I made a typo in the 'config\environment.rb' file above. I check. No typo. 2:52. Time for a break.

3:00. I think what I'll do is wipe out this install of ToDo and try to simply run it using Rails 0.13.1. It's only a .1 version change; it shouldn't be dramatically different. And, it's not like it's working on 0.12.1 anyways. So I do an 'rm -r' on rails/ToDo and wipe it out. I also kill the server, which this time seems actually to stay killed. The other server seems to have vanished as well, which means I can run the app on port 3000 again. Next, i run the 'rails ToDo' command. Then, I edit 'config/database.yml' as before. Then I start up the server using 'script/server -d'. Nope, address already in use. I try 'script/server -d -p 2999'. That works OK. I check the page in the browser and it loads fine.

Now to generate the model:

   [root@downes ToDo]# ruby script/generate model category app/models/
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/category.rb
      create  test/unit/category_test.rb
      create  test/fixtures/categories.yml

Piece of cake. Now for the controller. That mostly ran ok, except for:

   No such file or directory - ./script/../config/../app/views/category/app/controllers/.rhtml

What gives? Why is it seeking an empty .rhtml file? Weird. Let's check the directories. The files seem to be there, including 'app\controllers\category_controller.rb', which I am now supposed to edit. Here I get a bit of a change from the AddressBook code, using something called 'scaffold'. The author refers to the 'Rolling with Ruby on Rails' tutorial, which I read several months ago (and of which I remember very little - you rally have to play along when trying out these guides). Anyhow, I edit 'category_controller.rb' as instructed:

   class CategoryController < ApplicationController
      scaffold :category
   end

Now the file had a some other lines in it, which I deleted, as though it were testing to see whether (blank) exists. Hm. Anyhow, now I suppose a miracle is supposed to happen: "Point your browser at http://todo/category and marvel at how clever it is :-)". Sure. The 'Four Days' screen shows a nice table with a bunch of data. Here's what I get when I try http://www.downes.ca:2999/todo/category in my browser:

   Routing Error
   Recognition failed for "/todo/category"

What could it be? Maybe the URL is wrong? I try a few: http://www.downes.ca:2999/ToDo/category. Nope. http://www.downes.ca:2999/todoso/category. Nope. http://www.downes.ca:2999/todo/categorie. Nope. http://www.downes.ca:2999/todos/categorie. Nope. http://www.downes.ca:2999/todos/categories. Nope. http://www.downes.ca:2999/todo/categories. Nope. http://www.downes.ca:2999/todo. Nope. http://www.downes.ca:2999/todos. Nope. OK, it's not the URL. Check the database. It all seems to be in order.

Now I'm puzzled. What's different from this and AddressBook, which at this point was running fine? The port? Not a factor. The commands to generate the model and controller? Well, they seem different... yes, there's additional text after. Look: 'ruby script/generate model category app/models/' - the app/models' part isn't in the AddressBook guide. Check the Four Days guide. It's not there either. Wha? Where would I have gotten it from? Oh, I see - I cut and pasted the command from the PDF document. But cut and paste from the Linux PDF viewer is very buggy - and it added some text onto the end. OK, let's try the two commands again. yes, that works perfectly, and no error this time. OK, edit app\controllers\category_controller.rb. That extra stuff that I had to delete last time isn't there. Great! OK, let's try the app.

   Routing Error
   Recognition failed for "/todo/category"

Hm. 3:38 p.m. and I can't seem to get past this point. Now what's different? Two things: the name - 'category', which is an unusual plural (remember, Ruby does all sorts of flipping back and forth between singular and plural). And the 'scaffold' command, which, recall, was supposed to generate a miracle for us. Me, I am least trusting of miracles, so I should check back to see how we did it in AddressBook.

3:53. Maybe I should eliminate my own error as a possible cause. There may still be junk in the directories because of my bad generate commands. I wipe out the ToDo directory, then generate a clean install. Interesting. As I go through the steps again, I notice that the 'Four Days' guide never starts the server application. Hm. Does it miraculously work because of the mod_rewrite? I wonder, maybe the server persistently running on port 3000 might grant me access to the toDo list? Hm. I push on, but this time without starting up a server on 2999. 'rails toDo'. Then edit 'config/database.yml'. Then 'ruby script/generate model category' and 'script/generate controller category'. Then, vi into category_controller.rb and add the scaffold line. Then, just for fun, I try http://www.downes.ca:3000/ToDo/category. same error, "Recognition failed for '/ToDo/category'". It's not going to work, but for completeness I start up the second server with 'script/server -d -p 2999' and then try to access http://www.downes.ca:2999/ToDo/category. It doesn't work.

OK, so I look at the AddressBook script and I see two lines. There is a scaffold line, but it follows a 'model :contact' line. Could it be this simple? I enter category_controller.rb again and add the line 'model :category' just before the line 'scaffold :category'. Then I try http://www.downes.ca:3000/ToDo/category. Still doesn't work. Check the file again, to make sure there's no typos. No typos. Weird.

Maybe it's the use of 'category' as a table name. The plural for 'category' is odd - it's not merely 'categorys' but rather 'categories'. Now as I recall, Ruby is supposed to handle this - but maybe it doesn't. So let's change it around. I go into the database and rename the table 'categories' to' topics'. I also change the field name 'category' to 'topic' in the topics table. Then I edit app\controllers\category_controller.rb as follows:

   class CategoryController < ApplicationController
      model :topic
      scaffold :topic
   end

I then try http://www.downes.ca:2999/ToDo/topic in my browser and...

   Routing Error
   Recognition failed for "/ToDo/topic"

OK, well it doesn't appear to be the database name. Perhaps it's database access? No, it's exactly the same as for AddressBook... except... in that one, both the database and the rails directory have exactly the same name: AddressBook. But in the ToDo list, the directory is called 'ToDo' while the database is called 'todos'. Significant? Who Knows? But let's try to make the change. I create the 'ToDo' database and give all access. I then create the table 'categories' just as before. I edit database.yml and enable access. Then 'ruby script/generate model category' and 'script/generate controller category'. Then, vi into category_controller.rb and add the model scaffold lines. Then http://www.downes.ca:3000/ToDo/category and hit enter and...

   Routing Error
   Recognition failed for "/ToDo/category"

When all else fails, try Google. I execute this search and astonishingly come up with one result. The routing error is mentioned twice. In the first case, the recommended solution is to add the line:

   map.connect ”, :controller => “todo”, :action => ‘index’
to config/routes.rb. So I do that (adjusting 'todo' to be 'ToDo', to match the name of my database and rails directory. Of course that does nothing. The second instance seems more exactly like my problem:

   Mark McCray  said 29 days later:
   I’ve also had an issue where i get a routing error after following the instructions.
   I go to http://localhost/test then i get: Routing Error Recognition failed for “” Any ideas?
   I saw another comment about this above but i couldn’t get his suggestion to work for me.

Hopeful. But sadly, nobody ever replies to Mark McCray. Was his problem solved? Who knows! All I know is - mine sure isn't.

What else can it be? I can only think of one thing: that you can't run two Rails installations at the same time. I can't think of anything else it could be. Why else would AddressBook work perfectly while ToDo fails miserably? It's exactly the same set-up!

5:00 pm. Well, that's enough for today. Another lovely day on Rails.

5:30. Well, one more thing. I tried to run 'rake default' and got the following:

   [root@downes ToDo]# rake default
   (in /home/downess/opt-httpd-prefork-htdocs/rails/ToDo)
   /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.6.2/
      lib/rake/rake_test_loader.rb" "test/unit/category_test.rb"
   Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.6.2/lib/rake/rake_test_loader
   Started
   E
   Finished in 0.025179 seconds.
 
  1) Error:
   test_truth(CategoryTest):
   ActiveRecord::StatementInvalid: Duplicate entry '' for key 2: INSERT INTO categories (`id`) VALUES (2)
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:462:in `log'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/mysql_adapter.rb:121:in `execute'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:279:in `insert_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:278:in `each'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:278:in `insert_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:237:in `create_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:237:in `each'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:237:in `create_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:235:in `transaction'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:235:in `create_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:522:in `load_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:479:in `setup_with_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:504:in `setup'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:503:in `setup'
 
   1 tests, 0 assertions, 0 failures, 1 errors
   rake aborted!
   Command failed with status (1): [/usr/local/bin/ruby -Ilib:test "/usr/local...]

The good news: It's proof that something's broken. The bad news: it seems to be one of those gem modules, specifically, ActiveRecord. My little reinstall wouldn't have broken rails 0.13.1 adn well as 0.12.1, now would it have? I did a Google search, but this error appears to be unique. Hm. Looking at it. Look at the error: "Duplicate entry '' for key 2: INSERT INTO categories (`id`) VALUES (2)". Now I look at the database. " category_key UNIQUE 1" Drop this from the database. Run 'rake default again'.

   [root@downes ToDo]# rake default
   (in /home/downess/opt-httpd-prefork-htdocs/rails/ToDo)
   /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.6.2/lib/rake/rake_test_loader.rb"
   "test/unit/category_test.rb"
   Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.6.2/lib/rake/rake_test_loader
   Started
   .
   Finished in 0.025776 seconds.
 
   1 tests, 1 assertions, 0 failures, 0 errors
   /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.6.2/lib/rake/rake_test_loader.rb"
      "test/functional/category_controller_test.rb"
   Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.6.2/lib/rake/rake_test_loader
   Started
   .
   Finished in 0.02127 seconds.
 
   1 tests, 1 assertions, 0 failures, 0 errors

OK, that fixed the error. Does it fix Rails? No. 5:38 pm.

Continued in Part 3

Share |

Comments

Re: Ruby on Rails (2)

Hi

Your article is very useful . You must have had lot of patience to note down each problem you had and how you solved it . [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

[Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Thank you for porviding a usefull information. keep writing. Jim http://www.tatvasoft.com [Comment] [Permalink] [Previous][Next]

How I fixed my

At the "Welcome Aboard" splash screen at localhost:3000, there is a link called "About your application's environment". After clicking this I noticed my application root was in Trash! I had trashed an old version of my app and restarted. After emptying my trash and re-cd'ing to the app dir to run "script/server", everything was cool. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

My guess is it's something to do with specifying where MySQL is running on windows. ButI can't find any information related to that. IE, on OSX MySQL runs through mysql.sock and rails found it via the install of mysql and put the correct path everywhere. It doesn't appear to have done the same on windows. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

If you're talking to me that's not overly helpful. I've spent days reading every tutorial and piece of documentation I can find regarding installing rails on apache. :) I have it installed from scratch on OSX with Lighttpd, but it won't work on XP on Apache. At least for me. I'm yet to try on Apache on OSX. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

>To answer your question, to get Tomcat up and running, you do three >things: > >1. Download >2. Unzip >3. Run the start script me@mymachine:~# emerge rails (download/unzip) me@mymachine:~> rails test (setup devel enviro) me@mymachine:~> cd test; ruby script/server (startup script) => Booting WEBrick... => Rails application started on http://0.0.0.0:3000 => Ctrl-C to shutdown server; call with --help for options And dont get me started with any of the RPM or pkgsrc/ports systems which usually only require 1 command to install any given application with all of its dependencies. It is easy. Think of all the time you will save, not coding yourself into a cafe corner. You'll actually have time to comprehend a Unix Administration book! If your OS doesnt support such ease, its time to reconsider why you use something so complicated to "work". Unless, of course, you're russian. [ubuntu == localized] About your problem, i'd suggest reading proper documentation before you jump whole-heartly into something you know nothing about. This problem is most commonly attributed to a lack of knowledge. Even if that lack of knowledge, is reading the right documentation. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

My routing problem was because I had two virual servers running at hte same time on the same port. Removed one and now I get to a Mysql connection error (I can connect fine via webrick and on the console etc). "uninitialized constant Mysql" [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

I have this problem on Windows, trying to run rails under fcgi on Apache2. I've followed every guide i can find. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Hi Okke -- I'm having a hell of a time trying to get RoR running on my Ubuntu setup and that's how I stumbled on this post (I have the same "routing" error". To answer your question, to get Tomcat up and running, you do three things: 1. Download 2. Unzip 3. Run the start script The Ruby and Rails setup process if *far* more complicated than that. I've never had any problems setting up a Java servlet container. RoR might be easier to develop with (if I ever get that far) but at the initial setup leaves a lot to be desired. [Comment] [Permalink] [Previous][Next]

Recognition failed...me too

Same thing - wrong directory for launch if WEBrick! Been running earlier demos and didn't think to change directories. Arrrghhh! thanks! ak [Comment] [Permalink] [Previous][Next]

Recognition failed...

Don't know if you ever solved your 'Recognition failed...' issue but I was having exactly the same problem...until I realised that I was runnign WEBrick from the wrong directory. I'd been messign around with the ToDo tutorial first and decided to have a go with my own little application. Couldn't for the life of me get it working until I realised that I was running the server from the /ruby/Todo directory rather than my newly created dir. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

There is indeed a lot of distro-inflicted pain here. On Gentoo, I can assure you that once you run "emerge rails", it installs not only Rails, but RubyGems, Ruby and any other dependency that is supposed to be in there. Now, in theory, other distributions are supposed to set up the same stuff. In particular, Rails isn't so marginal that a distro could not fail to have a package for it, and still pretend to be a serious distro. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

(Same comment posted on other article about Ruby on Rails) Windows XP Pro install... Go to rubyonrails.org Click Download link connects to http://rubyforge.org/projects/rubyinstaller/ grab windows msi file Run downloaded file Program installed Finished in 9 minutes Go to http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html and read about how to get started...great place to start. posted by www.randyandgerri.com [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

To be pedantic, the 9 in kill -9 is not a 'strength indicator' it is the integer representing the POSIX signal SIGKILL. kill by itself defaults to SIGTERM (15) which requests the program to terminate itself. With SIGTERM the program can refuse to shut down either because its programmer decided so (using a signal handler) or because it is somehow out of control. SIGKILL is processed not by the program but by the OS which can simply unload the given process without undue politeness. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Well, I don't know about you, but I had none of these problems you are describing. I am running on a windows machine though. Wonder if you'll ever try to get a java server running without first getting your hands om some decent documentation. Guess that's quite improbable. Why are you trying to do the same thing with RoR then? Anyway, I think you're overreacting. - Okke Formsma [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Try http://localhost:3000/categories Cheers. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Stephen, I think you're generally right about the disconnect between the introductory Rails documentation available online, and the nuts-and-bolts of getting even a toy app up and running. I've been working with Ruby and Rubygems for quite a while now, and even after several weeks of working on a couple of Rails projects in my spare time, I'm still finding new little wrinkles and configuration tweaks to figure out. More importantly, I think that Rails is being somewhat oversold as an alternative to PHP or CGI, when it's really more akin to .NET or J2EE in complexity and scope. I've written a bit more about this subject on my weblog, (http://rcoder.net/articles/2005/10/21/rails-isnt-for-everyone) but I'd like to throw one additional suggestion out there: find a local (or not-so-local) Ruby or Rails group's mailing list, IRC channel, or whatever, and ask for help. The Ruby community is, in my experience anyway, one of the more supportive and friendly groups you could deal with. Good luck, Lennon Day-Reynolds [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Just to point out, you can ship a rails app to a system that has ruby installed, but not rails... Just include rails in the vendor directory using Svn you can do this with svn:externals, or you can use the rake freeze_edge command in .14 .. or manually dump the contents of rails into vendor/rails this is generally a good idea to do anyway, the webserver should not have rails installed, and then different apps can be running different rails versions if neccesary (so you don't have 'my admin upgraded rails and all my apps broke' problems) [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

FastCGI and lighttpd are not mutually exclusive. In fact lighttpd supports fastcgi scripts. Before you accuse the previous poster, you need to understand the technologies you are dealing with. About webrick, as someone said earlier it is not intended to be used in production; mostly only for developing on your local machine. Also, there is nothing forcing you to use port 3000. That is what webrick defaults to, but of course for deployment you should not use webrick and you can put your site on whatever port you want, but you are probably going to want to use port 80, the default HTTP port. Good night and good luck! Carl Youngblood [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

I've heard that Apache's mod_fcgi is buggy, I've never used it. There's also mod_fastcgi, it's different. I've used Lighttpd for Rails for a long time, it takes fewer resources and, in my experience, is faster. As for 'it just works', script/server just works. Deployment isn't that much more work, you can just set apache to use the CGI version as the error document, I don't see how that's hard. Like anything else it takes more work to get things working fast, PHP deploys quickly but there are ways to make it faster if you take the time to learn about them. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Thats funny previous commenter, you say you hear FastCGI is buggy...yet, "is easiest in my experience" but you go with Lighttpd. How moronic is that!?! I don't know if "it just works" is actually true given that kind of logic. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

I always find writeups like this interesting since I've never had any problems. Gentoo packages the latest Ruby and RubyGems for me, and Rails has always Just Worked. I guess I must be lucky. And to answer your question about FastCGI, normal CGIs are started and stopped for every request, so each request requires ruby/python/php/perl/c/whatever starting up, reading all the files, and shutting down. This is Slow CGI. FastCGI lets the script backend stay running and serve multiple requests, the code files are only read once and cached in memory. I have heard that Apache's FastCGI is a bit buggy, but I run all my apps through Lighttpd. For mod_rewrite - the documents you've been reading are out of date, mod_rewrite isn't needed any more, just set the rails handler to be the 404 document and it all works as it's supposed to. And finally, script/server starts WEBrick, a ruby-based web server. It's intended for development and testing purposes only, it's not an application server. Deployment of Ruby applications is normally done with FastCGI, or mod_ruby if you can get it to work. FastCGI is easiest in my experience. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Glad I could help, buddy! [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

try this instead of that 4 day pdf thing. http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=1 dc [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

bless you, chuck riley. you solved my silly problem. cheers [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

I would think it comes down to what you are buiding and what your level of expertise is. If you are inexperienced with programming, basic *nix practices, OSS practices, or web administration, stay away from rails for big projects. There are a lot of companies working with RoR, but their engineers aren't "newbs" by any stretch of the imagination. Rails isn't at 1.0 yet, but its awful close. It will be an evolving technology for a bit, granted, but in some cases its exciting to be the lightning rod ;-) [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

so steve the conclusion is that this is not ready for prime time ... [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Also, to "really really" kill a process, use "kill -9 [pid]" as root. kill without a strength indicator is liking asking someone politely to go unconscious, while kill with a "-9" is like hitting them with a steel pipe. [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

I was running into the same error message due to the fact that I was running a newer version of ruby and/or rails than rails 4 days suggested. After attempting http://localhost:3001/category/new rather than http://localhost:3001/todo/category the skies turned blue again. Might be worth a shot. peace. chuck riley [Comment] [Permalink] [Previous][Next]

Re: Ruby on Rails (2)

Hi I write because i have the same error than you, but only with one instance of server. I´m starting with ruby and rails so i don´t know too much about this. The issue appear in my case when run : ruby script/generate scaffold ToDo When run: ruby script/generate controller ToDo ruby script/generate model ToDo All works fine. I´m opening files looking for instances of static routes in files of own language, but i didn´t found something. I hope luky with your problem Francisco R. Navarro from Madrid Spain [Comment] [Permalink] [Previous][Next]

Comment



Title
Your comment:
Enter email to receive replies:

Your comments always remain your property, but in posting them here you agree to license under the same terms as this site (CC By-NC-SA). If your comment is offensive it will be deleted.

Automated Spam-checking is in effect. If you are a registered user you may submit links and other HTML. Anonymous users cannot post links and will have their content screened - certain words are prohibited and your comment will be analyzed to make sure it makes sense.