Rails
Developing web apps in Ruby on Rails
More on Rails
- Rails: authorization—Adding login/logout, roles and authorizations
- Rails: csv—Working with csv files
- Rails: documentation—ri and rdoc
- Rails: github—Using Octokit to integrate with github
- Rails: heroku—Deploying rails apps on Heroku
- Rails: rvm—Ruby Version Manager
- Rails: secrets—Keeping passwords, oauth client ids, etc. out of your github repo
- Rails: testing—TDD, BDD, Rspec, Capybara, FactoryGirl, etc.
- Rails: tutorials—A collection of useful tutorials
Ruby on Rails, often just called Rails is a web development platform based on the programming language Ruby.
Deployment
For apps developed in Rails:
Getting Started
These instructions use Ruby 2.3.3, which was reasonably up-to-date as of 12/30/2016.
While it is not strictly necessary to use rvm and bundler, it does make things considerably easier in the long term to start off your Ruby practice that way.
- Decide whether you are going to use
sqlite3 or
postgres` as your database.- It is MUCH EASIER to get this right the first time then to try to change horses in midstream.
- If you are just building a practice app on your local system, and will NOT NEED to deploy this in production later, it’s fine to stick with
sqlite3
, which is an easier database to work with. - BUT… if you are working with something you may want to actually deploy on Heroku, you should start with postgres right from the start.
- If using sqlite3, type
sqlite3 --version
to make sure that you havesqlite3
on your system.- On MacOS 10.5 and higher, sqlite3 is typically preinstalled.
- If using postgres:
- For MacOS, download from http://postgresapp.com/
- If it isn’t already there, install rvm on your system.
- It isn’t absolutely necessary, but it is very helpful, to install the Ruby Version Manager (rvm) first.
- Working this way, you’ll install
rails
through rvm. This allows you more flexibility in which version of rails you use—the rails version and ruby version are selected on a per project basis. - Using rvm allows you, when you are working on Rails project, to be always consistently use the same version of Ruby and of Rails each time you work on the project, even across different systems.
- Create and clone a new github repo.
- Make it public if you want to be able to use various deployment tools for free (Heroku, Travis-CI, etc.)
- Use the
.gitignore
for Rails
- In the repo, add a file called
Gemfile
with these contents:source "https://rubygems.org" ruby "2.3.3" gem 'rails', '>= 5.0.0.rc2', '< 5.1'
This is a temporary Gemfile to specify to rvm which version of Ruby and Rails we want to use, so that we can do each of the following things:
- check that
rvm
is working properly - install the “gem” for rails, but just for this project (rather than globally on our whole system)
- check that
- Use the
cd
command to change your current working directory to the top level of your repo.- If you are already in that directory,
cd ..
thencd
back into that directory. - The reason is that if
rvm
is working properly, this will signal that you should use the ruby version in the Gemfile, namelyruby-2.3.3
. - If you get the messsage that ruby-2.3.3 is not installed, type
rvm install ruby-2.3.3
to install it.
- If you are already in that directory,
-
Try
gem install bundler
- If that works, try
bundle install
-
Read about bundler here: http://bundler.io/v1.5/gemfile.html
-
It is quite common to see this error:
An error occurred while installing nokogiri (1.7.0), and Bundler cannot continue. Make sure that `gem install nokogiri -v '1.7.0'` succeeds before bundling.
- If you see that, just do what the error message says:
gem install nokogiri -v '1.7.0'
- If it still doesn’t work:
- If on Mac: you may need to install the command line tools for Mac, or a newer version of libxml2
-
-
You are ready to setup a new rails application. The command in the standard tutorial here is
rails new blog
, but we’ll do it a bit differently:- For postgres, use
rails new . --database=postgresql
- For sqlite3, use
rails new .
We use
.
so that the new application gets created in the current instead of in a subdirectory called, for example,blog
.This will overwrite certain files such
.gitignore
andGemfile
, and you’ll be prompted to see if that’s ok with you. Indicate that it is ok to overwrite them. The originalGemfile
we set up was just a temporary bootstrap. - For postgres, use
-
Now manually add the line
ruby "2.3.3"
into the newGemfile
after the linesource "https://rubygems.org"
- Try running these commands:
rake db:setup rake db:migrate rails server
Then visit http://localhost:3000 and see if you have a working rails app.
-
If you have a working rails app, commit the code to the github repo.
- You should be able to continue with the standard rails tutorial from step http://guides.rubyonrails.org/getting_started.html#hello-rails-bang. You may want to go back and look at the last part of step 3, which explains the structure of the Rails directories.
MacOS Problems
Problem: various bundle install things don’t work.
* Solution: Make sure you have the command line tools installed: `xcode-select --install`
Problem: Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/specs.4.8.gz)
* Solution: `rvm osx-ssl-certs update all`
More on Rails
- Rails: authorization—Adding login/logout, roles and authorizations
- Rails: csv—Working with csv files
- Rails: documentation—ri and rdoc
- Rails: github—Using Octokit to integrate with github
- Rails: heroku—Deploying rails apps on Heroku
- Rails: rvm—Ruby Version Manager
- Rails: secrets—Keeping passwords, oauth client ids, etc. out of your github repo
- Rails: testing—TDD, BDD, Rspec, Capybara, FactoryGirl, etc.
- Rails: tutorials—A collection of useful tutorials