Posts Tagged ‘rails’
Running The Depot Application With Jruby
I completed the Depot application from the Agile Development With Rails book, and I feel that I have a good understanding of the basics of developing with Ruby on Rails. It’s a great framework, and creating the Depot application was a fun and enlightening process.
I thought that it might be equally enlightening to “port” the Depot application to Jruby using the Jruby On Rails libraries. I had two basic milestones in mind:
- Development & Testing – I wanted to be able to develop and test my application just like I did with CRuby, using tools such as
mongrelandrake. - J2EE Deployment – The most popular process for deploying a Java-based application is to build a WAR file and then deploy it to a Java application server such as Tomcat (which was my choice). I wanted to be able to complete these steps with my RoR application.
The entire process was surprisingly easy. The first milestone, Development and Testing, is covered in this article. The J2EE Deployment milestone will be covered in a different blog article.
Development & Testing
Here are some simple steps that will have your application running with mongrel very quickly.
Please note that I am executing the steps below on a netbook running Ubuntu 9.10. These commands should work on most Linux distributions, however. Also, they should work with a few small adjustments on a Windows machine.
Prepping Your Project Directory
First, you’re not going to want to make these changes to your original version of the Depot code. You will probably want to create a copy of it that is designed to work with Jruby. Here’s how I did it on my machine:
$ cd /home/tom/Dev/ruby
$ cp -r depot depot-jruby
The location of your depot folder will probably be different on your machine, but I’m sure that you can make the necessary adjustments
Also, please note that it is a good idea to “freeze” your rails version now if you have not done so already. You can do this by executing the following command:
$ cd /home/tom/Dev/ruby/depot-jruby
$ jruby -S rake rails:freeze:edge RELEASE=2.2.2
Installing Jruby
Next, you’ll want to install Jruby. There’s a lot of ways of doing this, but this method worked pretty well on my development machine:
$ cd /tmp
$ wget http://jruby.kenai.com/downloads/1.4.0/jruby-bin-1.4.0.tar.gz
$ tar xvfz jruby-bin-1.4.0.tar.gz
$ sudo cp -r jruby-1.4.0 /opt
# please replace tom:tom with your user and group
$ sudo chown -R tom:tom /opt/jruby-1.4.0
$ sudo ln -s /opt/jruby-1.4.0 /opt/jruby
Then simply add /opt/jruby to your PATH, and see if you can execute jirb.
Installing Rails And Other Libraries
This process is just as easy using Jruby as it is using CRuby.
Install the gems using these commands:
$ jruby -S gem install rails --version 2.2.2
$ jruby -S gem install mongrel jruby-openssl jdbc-sqlite3
A few notes on those commands:
- Prepend the gem command with
jruby -Sto ensure that you are using the right version of thegemcommand. This also works with any other Jruby command, such asrake. - Since the example uses the 2.2.2 version of Rails, we’re going to need it too.
jdbc-sqlite3is the Java version of the sqlite3 DB driver.- The
jruby-opensslpackage is nice to have regardless of what you’re installing withrubygems.
Making Depot Safe For JDBC
You can’t use the C-based database drivers with Jruby, but luckily, the Jruby developers have made it very easy to use the JDBC-based equivalents.
First, make sure that you are using version 0.9.2 of the activerecord-jdbc-adapter driver or higher. You can check this by executing the following command:
$ jruby -S gem list | grep activerecord-jdbc-adapter
activerecord-jdbc-adapter (0.9.2)
Next, execute the following command from the root directory of your project:
$ jruby script/generate jdbc
exists config/initializers
create config/initializers/jdbc.rb
create lib/tasks
create lib/tasks/jdbc.rake
That’s it! Your Depot application is now ready to use Jruby-on-Rails.
Please note that I did not modify my config/database.yml file or hack any part of the Rails libraries to make JDBC work. These steps used to be necessary, but the command listed above gives you a much easier and cleaner way of using JDBC. For more information, please check out this article:
Test
To test my work, I did the following:
- I ran the
mongrelapp server and executed some manual tests of the Depot web site. That worked well. - I execute all of my tests using the
rake testcommand. This also worked as well as it had with the CRuby version of my application.
Success! Now it would be nice to review all of the files that changed or were added. Since I checked my project into my git repository before making any changes, I can get the information that I need pretty easily:
$ git status
# branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: config/initializers/jdbc.rb
# new file: lib/tasks/jdbc.rake
That’s it! No code or config changes were necessary.
Conclusion
As you can see, it’s fairly easy to “port” the Depot application to use Java-based versions of mongrel, rails, and the other libraries from the Agile Development With Rails book. In my next article, I will take this application and deploy it on top of a Tomcat server.
Review: Rails Deployment: Production Configuration and Advanced Rails Tactics
Rails Deployment: Production Configuration and Advanced Rails Tactics by Ezra Zygmuntowicz
My rating: 4 of 5 stars
Overall, this book had a lot of very good information, and it was very helpful to me as I deployed my first Ruby-On-Rails application into a “Production” environment.
Here’s the high points:
- Lots of good information on tools such as Capistrano, MySQL, Mongrel, Apache, and nginx.
- The authors clearly know what they are talking about.
- It’s helpful if you’re deploying a “toy” application (like I am) and if you’re deploying a large, clustered application.
- It doesn’t assume that you’re already an expert on either Web app deployment or Ruby-On-Rails.
Here are some of the things that could be better:
- This book was published over a year ago, and it already feels out-of-date. For example, there isn’t one mention of Phusion Passenger, even though this tool seems to be the new standard app server for Ruby-On-Rails in Production environments.
- This is very subjective, but I feel like the information could have been organized a little better. I felt as if the author jumped around a bit sometimes.
- Also, some of the passages were a little difficult to read due to their incorrect sentence structure. My writing isn’t perfect either, but I believe that the editor should have fixed more of these mistakes.
If you’re deploying a Ruby app in any setting, then this is a good book to get. I just don’t know if I would actually buy it.
Review: Agile Development With Rails, 3rd Edition
Agile Web Development with Rails: A Pragmatic Guide by Dave Thomas
My rating: 4 of 5 stars
In general, this book does a fairly good job of helping you create a rails-based application. Part 3 includes some great in-depth information on the topics that are briefly discussed in Part 2.
I only have one real gripe about this book. It packs in lots of topics (e.g. db theory, AJAX, unit testing, security, deployment), but it doesn’t really tell you much about them. Therefore, if you have a problem, then good luck figuring it out using the content in the book.
A good example is the final chapter which covers deployment. The chapter devotes only a few small paragraphs to configuring Apache for passenger. To me, this section was completely useless unless you were already an expert with Apache configuration. I ended that chapter with a broken Apache server and no resources (from the book) to begin fixing it.
Another problem that I had with that chapter is that it really didn’t follow the pattern that the chapters in Part 2 used. In those chapters, the authors would should you how to do something relatively small, show you how to test it, and then provide some troubleshooting information if the task was particularly complex. The deployment chapter gave you a *very brief and generalized* tutorial in each section, and then just assumed that everything went perfectly. It didn’t show you how to test anything, and it didn’t help you troubleshoot any possible problems.
Don’t get me wrong. I know that no book will provide all of the information that I would ever need about a subject, and thank goodness for the internet in these situations. I was just hoping that all of the chapters in a book that I actually bought would provide better information than some person’s blog.
So in general, I guess I would have to say that this was a very good book with some bad chapters that were tacked-on at the end.
Walking With Rails 1 – Setup On Ubuntu
Overview
One of my goals for 2010 is to gain a decent understanding of the Ruby On Rails framework. I don’t think that this knowledge will directly help me with my current job (as a FileNet P8 administrator), but I do think that it will provide some good benefits.
First, learning about something that’s a little bit outside of your comfort zone can often help you with your day-to-day tasks. For example, if you teach yourself the basics of Java development, then you will know how to read a stack trace. Having this knowledge is very important if you want to administer software written in Java.
Also, it’s just fun. Creating something useful out of a few lines of text never ceases to amaze me. Also, I find that learning new technical skills is good for your brain.
Finally, who knows what the future may bring? It never hurts to know as much as you can about popular technologies in general when you work in IT.
So to get started, I purchased a copy of Agile Web Development with Rails, Third Edition from the Pragmatic Programmers’ web site. This book seems to be one of the best for beginners, and I really love that you can buy DRM-free ebooks from the Pragmatic Programmers’ web site.
Installing Ubuntu Packages
So enough background information. Here’s how I set up my system to get started with Rails development. First, I installed all of the necessary software on my Ubuntu 9.10 machine that was available as an Ubuntu package:
sudo apt-get install rails ruby-full rubygems rake mongrel git-core sqlite3
Here’s why I installed more than just the rails and ruby pacakges. When you install Ruby from source on a Linux machine, you get a lot of software. In addition to the Ruby runtime, you get fun tools like irb, rubygems, rdoc, ri, and rake. However, when you install the ruby package on Ubuntu and Debian, you just get the Ruby runtime along with a a couple of libraries. Since I’m lazy efficient guy, I decided to install the ruby-full package instead to save a few keystrokes. Unfortunately, the ruby-full package still doesn’t include rake or rubygems, so I installed those manually.
Mongrel is a developer’s web server that is designed to work very well with Ruby on Rails development. Since I am also impatient, I wanted a web server that would work a little faster than WEBrick.
Finally, I installed git because I wanted to share code between two different computers. Git is a version control system that is very popular with Ruby developers, so I figured that I would use the most popular Ruby-related tools while I’m learning about Ruby.
Installing Software Without apt-get
Natrually, you can’t install everything that you need with apt-get. For starters, the version of rails that is used in Agile Web Development with Rails, Third Edition is 2.2.2, so let’s install that using rubygems:
sudo gem install rails --version 2.2.2
This operation took a little while (~ 7 minutes) on my machine for some reason.
Note: For some reason, this bog
Next, I’m a Vim user, so I would like to see if the rails.vim plugin would help me at all. There is an official Ubuntu package for this plugin called vim-rails, but when I try to install it using apt-get, I get the following error:
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:The following packages have unmet dependencies:
vim-rails: Depends: vim-full but it is not installable
E: Broken packages
When I tried to install the vim-full package on my machine, I got this error:
Package vim-full is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package vim-full has no installation candidate
Ok, so after all of that, let’s just install it the easy way:
(Download the latest version of rails.zip from the rails.vim download page)
$ cp rails.zip ~/.vim$ cd ~/.vim$ unzip rails.zip
Finally, open vim or gvim and execute the following command:
:helptags ~/.vim/doc
In case your interested, here are the other Vim-related packages that I have installed on my machine:
- vim-common
- vim-gnome
- vim-gui-commom
- vim-runtime
- vim-tiny
Conclusion
And that’s it! I now appear to have everything that I need to complete the beginning examples in Agile Web Development with Rails using Ubuntu 9.10 and Vim.
Update (1/20/10) – I added the sqlite3 package to the apt-get line above. This package is necessary if you want to query your database manually.

