You need to have a place where you can do your work this term.There are a few options available to you. You only need to do theone that works best for your situation. If you want the simplestsetup, then just use Cloud 9. If you want more control, and towork locally, then install to your OS.
The Simple Solution
The simplest solution to using Ruby on Rails is to use Cloud 9, which means you only need a web browser to do everything. The credit card information they request for registration is there to prevent spam at their end, and you should not be charged for its use. You should be able to use it freely. The details of this are explained in chapter one of the Rails Tutorial, which you should complete next as it covers all that you need to know to get started. RailsTutorial uses Cloud 9 too and will guide you through its usage. If you go with the simple solution, then skip down past the more complex solutions to the section onGit before creating a simple ‘boatyard’ app, and learn how to deploy your app to Heroku.
Use Ruby 2.5 and Rails 5.2 this term
We want to use Rails 5.2 this term. Check that Cloud 9 is set to create new applications using Rails 5.2.
If you already have ruby and rails installed, then you canupdate your ruby gem libaries and be good to go. First, checkthat you have the most recent version of RubyGems by using thecommand ‘gem update –system’ to update gem itself, then use’gem update’ to update all of your currently installed gems totheir latest version. Then move down to the section on Gitbefore creating a simple ‘boatyard’ app, and learn how to deployyour app to Heroku.
The More Complex Solution
If you’d rather have everything on your own machine, then use Go Rails to guide you through the appropriate steps for developing on Linux Ubunto, or Mac OS. The Pragmatic Studio folks also offer this guide to installing ruby and rails on Mac OS.
Windows 10 has the ‘Windows Subsystem for Linux’. This isn’ta full linux system. You still need to install a Linuxdistribution into this, and then install Ruby and Rails there.This is a good option if you’re on Windows. Just follow one of these guides and you’ll be set up. https://gorails.com/setup/windows/10 (Set up Postgresql for the database). This is also another described by Scott Hanselman too.
Do not try to develop Rails applications using Windows. This is not a good idea. It will add to your stress when things don’t work.
Using a Virtual Machine
VirtualBox will run a version of Linux for you inside yourWindows machine. To do this you need to download and ‘import’ this 32-bitWAD image to use with VirtualBox. Save the file as ‘WAD_Box.ova’ and now follow these steps carefully to install the WAD image. As it is a virtual machine, it doesn’t matter if your host machine is 32 or 64 bit for the guest image.
Don’t rush ahead, as you’re likely to miss an important step.Take your time…
a) Start VirtualBox and go to VirtualBox ‘File->ImportAppliance’ and select the file from where you saved it.
b) Select ‘choose’ and browse to where you saved theWAD-Box.ova file and then select ‘next’.
c) Change the ‘Name’ description to ‘WAD’ by double-clickingon it and select ‘Import’. When this is done you should seeWAD listed on the left in VirtualBox.
Start up our Linux Image
d) Before you can start the image you need to ensure that youhave a folder C:\vm_shared. If you don’t have it, then createit now. This is the folder where you can work on files in bothWindows and in Linux.
e) Highlight WAD and click on ‘start’. A few windows will popup warning about auto of the keyboard and then the mouse. Youcan just click them (you might also need to press ‘enter’ toget past the Grub boot screen) until you’re presented with theUbuntu Login screen.
Troubleshooting ‘black screen’ or ‘error messages’
If you press ‘enter’ to get into the Ubuntu generic systemand you have a black screen hanging with nothing happening, oryou might see an error about “VT-x/AMD-V hardware accelerationhas been enabled, but is not operational. Your 64-bit guestwill fail to detect a 64-bit CPU and will not be able toboot.” then you need to go back to the top of the page andlook at the ‘Check Virtualisation’ section, which you mighthave missed.
Using the Linux Machine
f) login with ‘ruby’ and the password ‘rubyrules’. You couldnow run everything from this one window, but that would bepainful as you’ll note there’s no scrolling available.Therefore we’ll start up a lightweight desktop. This will expand the window too, and let you run multiple terminal windows and other applications as needed.
g) type ‘startx’ and press ‘return’ to launch the desktop.
h) You’ll now have a black screen with a circle of threepeople holding hands and a notice that this is ‘workspace 1’in the bottom left. (You have 4 workspaces by default on Linuxworkstations.) You’re now ready to get started, but let’sfirst check that our connection to the internet is fine.
i) Right-click on the desktop and a menu will pop up and select ‘Google Chrome’ and then type in bbc.co.uk on the address bar. This should work. You can now look up materialsas needed. However, that is the one to use when off campus.
j) The editor you can use is on the main menu. Just ‘right-click->editor’ and this will open Scite. Type something into the editor and then use file->save and browse to ‘File system->media->sf_vm_shared and save the file as myfile.txt file.
k) Open up an editor in Windows and then open theC:\vm_shared\myfile.txt file and add something to it and thensave it. If you now switch back to the version in Scite youcan see the changes if you close the file and then open itagain.
l) Right-click->console on the desktop to open upTerminator, which is a nice console from which you have menuoptions if you right-click. You can split the screen, open uptabs and also use right-click to cut/paste this way too.
m) You should be able to find which version of Ruby is beingused as well as the version of Rails and a list of gems if youremember those commands. (They are: ‘ruby -v’, ‘rails -v’ and’gem list’ if you forgot.)
n) You can update the gems being used with the command ‘gemupdate system’, which will bring them all uptodate withwhatever the current version of ruby supports.
o) You can update the version of ruby being used with thesecommands in the console. Just open the console and followthese steps:
cd to .rbenv
cd to .rbenv/plugins/ruby-build
(you should now be back at the root directory where you startedand see .rbenv if you type ls -al)
rbenv install -l
(this will give you a long list of possible ruby versions andyou want to scroll back to see ruby 2.5.x or whatever is latestto use for the next step)
rbenv install 2.5.1 (or whatever version you want – thiw willtake a bit of time to do)
rbenv global 2.5.1 (or whatever you used in previous step)
ruby -v (should now show your new version)
Now you can install the gems you want for Rails development:
gem install rails
gem install rspec
gem install rspec-rails
gem install cucumber
gem install cucumber-rails
gem install sqlite3
gem install capybara
gem install heroku
For the future… when you’re working with apps on Heroku, then you might be interested to know that Postgresql database is already installed. It is currently set to not start for autostart. You can enable this later using the command:update-rc.d postgresql defaults
The user for psql is postgres as by default and is set to start automatically. The password for the account is set to ‘ruby’.
Create ssh keys
In order to work with Heroku and other Git repositories weneed ssh keys so let’s create some for this machine to add toour account. Otherwise you’ll need to always use the one onyour Windows machine. If you don’t have a Heroku account, thengo create one as you’ll need it eventually when working withRuby and Rails.
n) Type the following command in the console to create a newkeypair. Follow the instructions, you just need to press’enter’ when it asks for a filename, and add a passphrase, orpress return if you don’t want one.
ssh-keygen -t rsa -C “firstname.lastname@example.org”
o) You can now cd into the .ssh directory and add the new keywith:
It will then ask for your email address and your heroku loginpassword.
p) To shut everything down close the windows and thenright-click->exit on the desktop. This will drop you backto the regular console window.
q) Use ‘Machine->Close’ from the main menu and then select’Send the shutdown signal’ and click ‘OK’ and this will closethe virtual machine.
That’s it. You now know what to do with your ruby and railswork and will no doubt find a suitable workflow for using thiswithout the frustration of running Ruby under Windows andfinding gems don’t build,or work as expected.
You’re now done and should be able to do all that you need todo in a Linux box on a Windows machine. This should take lotsof the pain of Ruby running on Windows away. So that you havea better idea of what you’re doing you should also look tothese links for more help:
https://help.ubuntu.com/community/UsingTheTerminalfor more on commands.
http://fluxbox.org/for details about the windowing manager installed, and how tomodify it for your own taste.
Git is also needed for working with Ruby and Rails
Be sure to also set up you ssh keys too, as you’ll need these later. You can find more about using Git at https://git-scm.com/book/en/v2, and you can also read the details of deploying to Heroku with Git.
Testing Your Rails Solution by building a bigger app and deploying it to Heroku
To ensure that all of this works correctly we should now createa simple app. We’ll do this by creating a boat yard, or marinaof boats.
Open a terminal and cd to where you want to keep all of yourRails applications. I keep mine under a folder called’RailsApps’ so they’re all in one place.
Move into this space in a terminal, and then enter the command:
rails new boatyard
This will generate a new directory with many files in it.
Move into the new ‘boatyard’ folder with your terminal. We havelots of folders and files here that will help us keep ourapplication development organised so that it’s easier tomaintain.
Next we want to create some boats using the built in scaffolding of Rails. We can use one command to build a boatmodel, connect it to a controller, and associate it those with aview to display on the page as well. Type out the following intoyour terminal:
rails generate scaffold Boat name owner harbour
This will again generate more files for you, which you shouldsee as being part of the boat objects.
The next step is to tell the app about the new database information for our boatyard objects with the command
This should create the ‘boats’ table.
We can now start the server and see our work with the command
(If you’re using codio, then use rails server -b 0.0.0.0 and then open the ‘box url’ from the drop down options next to ‘Project Index (static)’ into a new browser tab.)
This should tell you that Puma is starting in single mode and is listening on tcp://0.0.0.0:3000
Open your browser and go to http://localhost:3000. Hang on, that’s not your app is it? All those happy people around the globe? This is the ‘standard’ welcome page to show your app isready and running. We haven’t created a ‘home’ page for the app.We have only created a page for the boats. To see the ‘boats’you need to go to http://localhost:3000/boats where you can add a ‘new boat’. Fill in the form and create a boat. See whathappens if you create a boat without filling in any details.
This should all work. It’s not pretty, but it works and helps you quickly create a website. You can shut down the server withthe command ‘ctrl-c’ which you do by pressing down both the’control’, or ‘ctrl’ key and the ‘c’ key at the same time.
Pushing your boatyard to Heroku
In order to push our application to Heroku, we need to put itinto a Git repository. Heroku only acceptions applicaitons asGit repos. For now just follow the commands to ensure that youhave everything set up correctly. We’ll go over details in alater session.
We also need to change our Gemfile before we can push the application to Heroku because it uses Postgreql instead ofSqlite3for the database.Your gemfile details the gems used in your application, and as we want to use Postgresql for production, we need to include that in the file. Open boatyard/gemfile and add theselines:
group :development, :test do gem 'sqlite3' gem 'byebug', platform: :mri end group :production do gem 'pg' end
Also comment out any reference to sqlite3 in the main body of the Gemfile. Then copy that and move it to the ‘development and test’ sections next to ‘byebug’ gem, then save the file.
Step 4) You need to run bundle install to add the gem. This might take a while.
Given you’re still in the boats directory use
git add .
git commit -m ‘my first commit’
This should set up your repo, fill it, and then commit the codeto it, and show your local git repository working fine.
Sign up for an account at http://heroku.com and then follow the instructions at https://devcenter.heroku.com/articles/getting-started-with-ruby#set-up. (If you’re on cloud 9, or codio, and heroku isn’t already installed, then use the ‘other installation methods’ for installing the CLI.
If you don’t have an ssh key in your .ssh directory, (which you probably don’t), then you will need to create one using the details at github. This is an extra layer of security over and aboveyour username/password combo.
Upload the ssh key you created for use with Git when prompted.NOTE: when uploading your ssh key supply your heroku password when prompted for a password.
You may need to supply your public ssh key manually in orderfor this to work. Your ssh keys will be under <youraccount>./ssh then you need to use this command inside a Git bash shell after you’ve used to cd to get to the .ssh directory:
heroku keys:add id_rsa.pub
It will ask for your email and Heroku password and then upload the file.
You can now create yourapp on the heroku server and then upload your app. Be patient during this phase as you are working remotely so give the servers time to respond to your commands.
Use the ‘heroku create’ command inside the boatyard directory to create your app on the server and get its default name. You should see that heroku adds a remote repository to yourgit repository for remote deployment and tells you what the URLwill be.
If the heroku create command doesn’t add your remote repository details, then you can run the command yourself with
git remote add email@example.com:your-app-name.git
you can now push the app to heroku with
git push heroku master
Do this now and you’ll see a quick message about adding an RSAhost for an IP address …. and then details about writing yourobjects to the repository and then it will configure the gems itneeds to run your Rails application. This may take a bit oftime, so be patient. When it is done it will launch your application and tell you the URL it’s it running at.
If you go to the URL, you’ll see the ‘We’re sorry, but something went wrong message’ as you’ve not configured the database yet.
We need to tellheroku to create our tables by using these commands:
heroku rake db:create
heroku rake db:migrate
You will probably have a message that some weird database namealready exists after the first command, and the second commandshould show your tables being created. If you now reload thepage where your app is, then it should appear there correctly.
Extending the practical
If you still have time, then you can also explore these otherchanges with the boatyard to become more familiar with Rails.We’ll cover this later too, of course. The main documentationfor modifying views is found in ‘Layouts and Rendering Guide‘ for Rails.
The main folder to explore is the ‘boatyard/app’ directory,which is where you’ll find the ‘views’ folder, which holds your’boats’ and ‘layouts’ views.
The ‘layouts/application.html.erb’ file is applied to all viewsin the browser so changes here impact the whole site. You mightwant to show the name of the whole site across all pages.
The ‘boats’ folder holds the ‘index.html.erb’ file, which iswhat’s shown when you see the list of all boats. It also holdsthe show.html.erb file, which is used to display a single boat.You could change how boats are displayed, and maybe add moredetail of how single boats are displayed.