Ruby on Rails
Contents
Installation
I just find it easier to do things in Linux. On a Windows machine, go on WSL. There is a Ruby installed but you probably want the most recent versions of Ruby and Rails. Get it from the RVM website. Next, you will need to
- Develop the app with Rails
- Publish it on Heroku
Development in Rails
Start from this guide. It is quite long but it introduces the MVC (Models, Controllers, Views) concept that Rails follows very well. There are other sources on youtube that follow the same steps as the guide.
So what is MVC?
Models
Let's start with the M. Models are objects to be stored in a database. Generating models goes like this
rails generate model Name column1 column2:string column3:text etc
Where the data type for column1
is assumed to be a string if left blank. Notice how Name is singular and capitalized, that's a convention.
Models are located under /app/models/
. If you want to see the schema of the database, check out /db/schema.rb
.
Views
Generating views using HTML templates and embedded Ruby e.g. index.html.erb
. These views correspond to controller actions (written controller#action
).
Under views, you might see @variable
syntax. These are variables have been defined previously in the controller.
While on the topic of syntax, Rails uses symbols to identify things. In particular, it uses them as keys when naming method parameters and looking things up in hashes. You can think of symbols as string literals that are magically made into constants.[1]. For example
redirect_to :action => "edit", :id => params[:id]
Views are located under /app/views/
.
Controllers
Controllers define actions based on the HTTP requests that the user makes.
To generate a controller, type
rails generate controller pages
The above generates a pages
controller under app/controllers/pages
.
Routing
This is where the action starts from the user's perspective. The user inputs an http request, the predefined routes (in /config/routes.rb
) perform a controller action, possibly interacting with the database and generating a variable or more, then take the user to the view. For example, /config/routes.rb
contains this
get 'about' => 'pages#about'
to send the user to an "About" page. The controller has something like
class PagesController < ApplicationController
def about
@text='about me'
end
end
And the view will have
<%= link_to 'index', root_path %>
<h1>About Me</h1>
<%= @text %>
RESTful Resources
A resource is the term used for a collection of similar objects, such as articles, people, or animals. You can create, read, update, and destroy items for a resource and these operations are referred to as CRUD operations. To define a REST resource, In config/routing.rb
, under Rails.application.routes.draw do
write
:resources :quotes
The Database
The database configurations are stored in the file /config/database.yml
. Assuming you are going to deploy the app on Heroku, which uses postgres, here are settings that are known to work
development:
adapter: postgresql
database: my_database_dev
pool: 5
timeout: 5000
test:
adapter: postgresql
database: my_database_test
pool: 5
timeout: 5000
production:
adapter: postgresql
username: myrails
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Publishing to Heroku
Heroku provides a guide [2] on publishing rails developed websites on their platform.
Big Issue
I read every article that had the words Heroku and Rails in it but couldn't get the database to connect. This is the command that finally did it[3]
heroku run rake db:migrate
Thanks codeacademy! Changing the environment to production might have been needed first[4]. Here is how to check that
$ rails console Rails.env.production?
There is a lot more to say about deployment. I will put some placeholders here.
Diving Deeper
Rails functions
link_to
form_with
rake
rack
etc
The Active Record
Further Reading
- Japanese source. Setting up
database.yml
for rails to work with heroku.