FINALLY! SOME UPDATES! This website is being reconstructed. Some content will go away soon. If you want to see my new stuff, visit my github: https://github.com/jrcharney/.

January 14, 2014

Rails and Node.js

First post in nearly two months. My header is undergoing some changes, but I think it's time to settle a few matters with getting Ruby on Rails and Node.js running.

Node.js is the ideal software platform for working with Rails, because Rails needs a JavaScript engine and Node.js is happy to oblige.

So let's put this thing together!

Firstly, I don't think we need to stress how much Ubuntu (or several other Linux distros for that matter) loves to get people to use their repositories to download software especially when their repos are out of date most of the time. So, to avoid this, let's get our Node.js from a more reliable PPA. Chris Lea gladly provides this. So before we install Rails, let's install Node.js using Chris's PPA.

$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs
Installing Node.js is easy!

One of the things I like about Node.js is the fact that it has a REPL just like Google Chrome has. A REPL is an interactive environment just like Bash, Ruby, Perl, and Python has. In fact, if yo want to try out some REPLs interactively, you can go to REPL.it and give Ruby, Python, and other REPLs a test drive. When you have Node.js installed, you can boot it up and play around with it like so.

$ node
> 2 + 2
4
> "All your base " + "are belong to us."
'All your base are belong to us.'
> partyRock = "Everyday I'm shuffling";
'Everyday I\'m shuffling'
> partyRock.split(" ");
[ 'Everyday', 'I\'m', 'shuffling' ]
> 
(^C again to quit)
> 
$
To exit from node, type CTRL+C twice! Also, Node.js prints output IN COLOR!

Now that we have Node.js installed, let's take care of the other half of this post.

Hopefully, you've been reading my previous posts for how to properly install Ruby and JRuby. Here we use Ruby's gem program to install Rails, because using rvm is really only good for installing Ruby and jRuby (everything else is such a pain in the butt!). Using apt-get is also foolish! Besides, what did I just say about using Ubuntu's repos! On RubyGems.org there is a Rails gem. We'll be using that to execute the following installation commands.

$ gem install rails
That's it?

'Fraid so, figcaption element. However, there are somethings that are really REALLY important that I should point out.

  1. This gem will take some time to install. Do not press CTRL+C. Do not Google "installing ri documentation for rails-4.0.2 stuck" because that is part of the process. Do not pass Go. Do not collect $200. Do something productive because this will take a few minutes. Go make a sandwich or find that old HTML5 Canvas project you did, no that other one--and give it a good update.
  2. You do not need to be root to install gems. In fact, go tell sudo to make you that sandwich. Administrators might want to take heed of this...not that you have anything to worry about. *cough*

Lastly, there's the issue of getting Rails running. I find putting anything where I need to test stuff on 127.0.0.1 (a.k.a. localhost) where it should go, in /var/www/.

Rails is quite new to me, and I hope to write up a few more blog entries for how to do stuff with Rails this year especially in the next few weeks.

First, because I can, I want to do all my Rails stuff inside a special sandbox folder called railyard where I will set up a project called train. Because I can use sudo, I can also create this file in /var/www/.

$ cd /var/www/
$ sudo mkdir railyard
$ sudo chown jrcharney railyard
$ sudo chgrp jrcharney railyard
$ cd railyard
$ mkdir train
$ rails new /var/www/railyard/train/
$ rails server
ALL ABOARD!

So here's what happened. We told Rails to create a new project at /var/www/railyard/train. I'm not sure if I need to do mkdir train, but everything is working, so I'm happy with it. We then told Rails to start up the server. If everything is working, Rails should keep running in the prompt (which a good rails server & (with an ampersand) will let it run in the background), and you should see a Rails boilerplate page at http://localhost:3000/ since Rails is listening to port 3000. I'm sure there is a way to forward port 80 traffic, but for now it's running. Rails will stop if you type CTRL+C. If that happens, localhost:3000 will not become available, much like what happens when you stop the Apache HTTP server when you run sudo service httpd stop.

There are two other things I need to look into. Firstly, I need to see if Rails uses Apache HTTP Server or if it uses something else like Nginx. Secondly, what database server Rails uses. Rails claims that SQLite is the default SQL database engine, but I still have MySQL available (and phpMyAdmin) though I'd really like to give MariaDB a spin. Maybe if I got the time, I can just start over from scratch and put it all on a Raspberry Pi. Ray Hightower did it although now he recommends using a BeagleBone Black. There's a lot of stuff I would like to do this year like build a 3D printer out of old computer parts. If Kodjo Afate Gnikou can make one for about $100 in Togo, why should the folks who produce all that e-waste keep sullying the third world with perfectly usable technology?

Like I said, there's much to do still. The year is still young.

Tags

Under Construction