Tuesday, March 29, 2011

Getting my Rails3 App Up and Running for the First Time on Site5

Currently the key documentation for getting a Rails 3 application working when you're using Site5 for your hosting is: http://www.site5.com/blog/programming/how-to-enable-rails-3-0-on-site5/20101007/.

When I log into my web site at site5 at a secure shell prompt, I see the following by default:
  • Rails version 3.0.1
  • Gem version 1.3.7
  • ruby version 1.8.7 (2009-06-08 patchlevel 173) [x86_64-linux]
My application was built using Rails 3.0.5 and expects that version -- not 3.0.1. But that's not a problem. Here's what I did to get it running on my web site hosted by Site5:
  • Set my .bashrc and .bash_profile files up following the Site5 article, although I chose to put most of it in .bashrc rather than .bash_profile. I found when it was in .bashrc, it was set when I logged in at a command prompt where I wanted it to be. My .bash_profile sources .bashrc anyway.
  • Set my .gemrc file up just as the article says.
  • Renamed the existing public_html and linked my app's "public" directory to public_html as the site5 article says.
  • cd to my app directory and ran: "bundle install --path vendor/bundle" to install Rails 3.0.5 and all other dependent gems my application needed.
  • Managing my web site using the excellent Site5 web tools, I created my development MySql database and added my app's development user and set its database permissions.
  • I modified my app's "database.yml" file according to the database and user I had created. I made the configuration of "production" and "development" identical, because when Site5 runs my app, it's running in production mode. I don't care about having a separate database at the moment. I'm just looking for proof of concept.
  • In my application directory from a command line I ran "rake db:migrate" to create all my tables and indexes. This ran as usual in the default "development" configuration, but since I configured my database connections as noted above so that "development" and "production" connect in the same way, it's OK to operate in the default mode.
  • In my apps "public" directory I created the .htaccess file as the Site5 article shows.
  • And again following the Site5 article, I "restarted" my application my giving the command "touch tmp/restart.txt" -- that's how to tell the server to restart your application.
And that was all I had to do. And by the way: Site5 has excellent support ready to carry on a live conversation or respond to your support ticket -- and they respond fast! (Even on Sunday mornings US Eastern Time when I'm often working on this.)


Tuesday, March 22, 2011

Upgrading Rails 2.3.5 App to 3.0.x: Continued

This post is a work in progress. I will add to it as I progress bringing my app up-to-date.

Now that my application is working again (see my previous post). I'm going through and upgrading various bits of things that will be deprecated:

  1. RAILS_ENV becomes Rails.env, RAILS_ROOT, Rails.root.
  2. <% form_for ... %> and <% form_tag ... %> become <%= form_for ... %> and <%= form_tag %>
  3. Hour.find(:all, ...) becomes Hour.all
  4. Hour.find(:all,:conditions => ["user_name = ?",user_name],:order => 'date_of_service desc',:limit => limit) becomes Hour.where("user_name = ?",user_name).order('date_of_service desc').limit(limit)
  5. We don't need the rails_upgrade plugin any more, so remove it: rails plugin remove rails_upgrade
  6. In application.rb: "config.smtp_settings" becomes "config.action_mailer.smtp_settings"
  7. Move a class that subclasses ActionMailer::Base: mv app/models/volunteer_mailer.rb app/mailers
  8. VolunteerMailer.deliver_message_to_volunteer_coordinator() becomes VolunteerMailer.message_to_volunteer_coordinator.deliver()

Saturday, March 12, 2011

Upgrading Rails 2.3.5 App to 3.0.x: From Frustration to Initial Success

I'm migrating a tried and true Rails 2.3.5 app to Rails 3. It's been a frustrating and enlightening project. The main frustration is my impatience. It's not all that hard, but since I'm doing this in my spare time, I wanted instant gratification, and I didn't get it. However, I'm finally to the point that the app is up and running, and I'm just shaking out more of the little things as well as brining it more in compliance with future upgrades that we know will be coming. Here are some notes (My app is named "pferd", the German word for horse, which stands for the Pegasus Farm Electronic Relational Database. When you see "pferd" that's my app name). I'm sure I'll be posting more. But this much got me to a functional application.
  1. Read some key articles, like:
  2. Bought Agile Web Development with Rails. This has proven to be a great quick reference. I'll read it thoroughly soon.
  3. Made a good backup of my existing app, and then copied it to "pferd.prev" so I can continue to run it every now and then to compare with the new version.
  4. Installed the latest rails: gem install rails.
  5. In my as yet still Rails 2 application and with Rails 2.3.5 still installed on my system, I ran: script/plugin install git://github.com/rails/rails_upgrade.git
  6. Captured the notes this install process puts out.
  7. Per those instructions ran: rake rails:upgrade:check
  8. Per the same instructions ran: rake rails:upgrade:backup
  9. Captured the notes this process generates for future reference.
  10. In the root directory of my app, ran: "rails new ." and confirmed that I want to overwrite all.
  11. NOTE: at this point I now have a Rails 3 app, and the upgrade plugin rake commands are no longer guaranteed to work. But since I still have "pferd.prev" which is still a Rails 2 application, I can still run the usual commands there as long as I need to. I have a Rails 3 application, so I have to start running Rails 3 compliant commands. I run "rails sever" for instance, instead of "script/server", etc.
  12. Installed "mysql2" gem: gem install mysql2.
  13. Renamed public/index.html created by the "rails new" command above so the app starts where I want it to start.
  14. Edit "Gemfile" to get rid of reference to sqlite3 and add "gem 'mysql2'"
  15. In my Rails 2 version ("pferd.prev") ran: "rake rails:upgrade:routes" and used the output of that command in config/routes.rb in my Rails 3 application.
  16. Created a new database migration to delete column "id" from all "join" tables (tables used in many to many relationships as specified by the "has_and_belongs_to_many" helper methods). Without this you get the error: Primary key is not allowed in a has_and_belongs_to_many join table ... .
  17. Restored app/views/layouts/application.html.erb from pferd.prev.
  18. Added call to "super" in all the controllers I'd created in which I had my own "constructor" (the initialize method). Without this, ApplicationController was not properly initializing my pages. It was not calling app/views/layouts/application.html.erb. The most obvious side effect was that none of my defined styles were coming in from my pferd.css style sheets file.
  19. I use "will_paginate" to paginate lists of records. It was broken after the upgrade. To fix it, I added "gem 'will_paginate',  '>=  3.0.pre'" to the Gemfile and then ran: "bundle install" which installed "will_paginate (3.0.pre2)" for me. I guess I might have used this method for mysql2 above also, but I did not.
  20. Ran "rails plugin install git://github.com/rails/dynamic_form.git" to prevent "DEPRECATION WARNING: f.error_messages was removed from Rails and is now available as a plugin."
  21. Replaced RAILS_ENV with Rails.env and RAILS_ROOT with Rails.root everywhere.

Netbeans 6.91 and Rails 3

My long time favorite IDE for Ruby and Rails has been Netbeans. And more specifically, I really like the code editor. I rarely use anything but the editor. I don't typically call upon the IDE to do much else besides edit my code for me. I realize that support for Ruby and Rails is going away or has already gone away. However, there is an effort to keep that going. And Netbeans 6.9.1 continue to run just fine and it still has ruby and rails in it.

So now that I'm moving on to Rails 3, how is the old Netbeans doing? Superb! So far for what I've asked it to do, it just does it. And I'm glad it nags me about things like replacing RAILS_ENV with Rails.env.

BTW: I tried RubyMine, but it was not as good as Netbeans and would have cost me $60.00. Maybe I'd change my mind if I switched over to RubyMine for a week or so to really get acquainted with it, but a few minutes with the evaluation copy did not give me enough motivation to leave Netbeans.

Wednesday, March 2, 2011

Moving My Website to Site5

Over the weekend I moved my web site from myhosting.com to Site5. I wish I'd done that years ago. It's been over ten years since I did anything like this. It's a lot easier now than it was then. And Site5 is a pleasure. I get so much more for less money than I ever did before. My primary motivation is to put up a Ruby on Rails application. I could have done with my former provider -- if I'd cared to spend more money. Instead I'm spending less and getting that and other benefits (like ssh into my web site right out of the gate).

I also found out the hard way about an unfortunate policy of my former provider: you never get a refund for the service you pay if you cancel before the end of the term. So, if you were doing them and yourself a favor and happened (as I did) to pay annually, you wait till the end of that 12 month term. If you were paying monthly you're out of some money. I had been with them for years and years and had nothing bad to say about them, but that policy is very unfortunate, and there's no budging them. I get credit toward other services -- which I have no use for. The best I can do is apply the money I spent to domain renewal. That will take me a long time.

Oh well. I'm so happy with site5 I can't say I really care.