Getting started with Ruby on Rails
Create a new Rails app with a non-standard database adapter
Rails is shipped by default with
ActiveRecord, an ORM (Object Relational Mapping) derived from the pattern with the same name.
As an ORM, it is built to handle relational-mapping, and more precisely by handling SQL requests for you, hence the limitation to SQL databases only.
However, you can still create a Rails app with another database management system:
- simply create your app without active-record
$ rails app new MyApp --skip-active-record
- add your own database management system in
gem 'mongoid', '~> 5.0'
bundle installand follow the installation steps from the desired database.
In this example,
mongoid is an object mapping for
MongoDB and - as many other database gems built for rails - it also inherits from
ActiveModel the same way as
ActiveRecord, which provides a common interface for many features such as validations, callbacks, translations, etc.
Other database adapters include, but are not limited to :
Create a new Rails app with your choice of database and including the RSpec Testing Tool
sqlite3 as the default database, but you can generate a new rails application with a database of your choice. Just add the
-d option followed by the name of the database.
This is a (non-exhaustive) list of available database options:
The -T command indicate to skip the installation of minitest. To install an alternative test suite like RSpec, edit the Gemfile and add
Then launch the following command from the console:
Creating a Ruby on Rails Application
This example assumes Ruby and Ruby on Rails have already been installed properly. If not, you can find how to do it here.
Open up a command line or terminal. To generate a new rails application, use rails new command followed by the name of your application:
If you want to create your Rails application with a specific Rails version then you can specify it at the time of generating the application. To do that, use
rails _version_ new followed by the application name:
This will create a Rails application called
MyApp in a
my_app directory and install the gem dependencies that are already mentioned in
To switch to your newly created app's directory, use the
cd command, which stands for
my_app directory has a number of auto-generated files and folders that make up the structure of a Rails application. Following is a list of files and folders that are created by default:
|app/||Contains the controllers, models, views, helpers, mailers and assets for your application.|
|bin/||Contains the rails script that starts your app and can contain other scripts you use to setup, update, deploy or run your application.|
|config/||Configure your application's routes, database, and more.|
|config.ru||Rack configuration for Rack based servers used to start the application.|
|db/||Contains your current database schema, as well as the database migrations.|
|Gemfile Gemfile.lock||These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem.|
|lib/||Extended modules for your application.|
|log/||Application log files.|
|public/||The only folder seen by the world as-is. Contains static files and compiled assets.|
|Rakefile||This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails.|
|README.md||This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up etc|
|test/||Unit tests, fixtures, and other test apparatus.|
|temp/||Temporary files (like cache and pid files).|
|vendor/||A place for all third-party code. In a typical Rails application this includes vendored gems.|
Now you need to create a database from your
Now that we've created the database, we need to run migrations to set up the tables:
To start the application, we need to fire up the server:
By default, rails will start the application at port 3000. To start the application with different port number, we need to fire up the server like,
If you navigate to http://localhost:3000 in your browser, you will see a Rails welcome page, showing that your application is now running.
If it throws an error, there may be several possible problems:
- There is a problem with the
- You have dependencies in your
Gemfilethat have not been installed.
- You have pending migrations. Run
- In case you move to the previous migration
If that still throws an error, then you should check your
Creating Rails APIs in JSON
This example assumes that you have experience in creating Rails applications.
To create an API-only app in Rails 5, run
Add active_model_serializers in Gemfile
install bundle in terminal
ActiveModelSerializer adapter to use
Generate a new scaffold for your resource
This will generate the following files:
Generate a Resource with Scaffolds
Instead of generating a model directly . . . let's set up a scaffold. A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
Here's an example of scaffolding a resource called
Task with a string name and a text description:
This will generate the following files:
example to delete files generated by scaffold for the resource called
Generating A Controller
To generate a controller (for example
Posts), navigate to your project directory from a command line or terminal, and run:
You can shorten this code by replacing
g, for example:
If you open up the newly generated app/controllers/posts_controller.rb you'll see a controller with no actions:
It's possible to create default methods for the controller by passing in controller name arguments.
To create a controller within a module, specify the controller name as a path like
parent_module/controller_name. For example:
This will generate the following files:
A controller is simply a class that is defined to inherit from
It's inside this class that you'll define methods that will become the actions for this controller.
Installing Rails on Ubuntu
On a clean ubuntu, installation of Rails should be straight forward
Upgrading ubuntu packages
Install Ruby and Rails dependecies
Installing ruby version manager. In this case the easy one is using rbenv
Installing Ruby Build
Installing Rails on Windows
Step 1: Installing Ruby
We need Ruby programming language installed. We can use a precompiled version of Ruby called RubyInstaller.
- Download and run Ruby Installer from rubyinstaller.org.
- Run the installer. Check "Add Ruby executables to your PATH", then install.
- To access Ruby, go to the Windows menu, click All Programs, scroll down to Ruby, and click “Start Command Prompt with Ruby”. A command prompt terminal will open. If you type
ruby -vand press Enter, you should see the Ruby version number that you installed.
Step 2: Ruby Development Kit
After installing Ruby, we can try to install Rails. But some of the libraries Rails depends on need some build tools in order to be compiled, and Windows lacks those tools by default. You can identify this if you see an error while attempting to install Rails
Gem::InstallError: The ‘[gem name]’ native gem requires installed build tools. To fix this, we need to install the Ruby Development Kit.
- Download the DevKit
- Run the installer.
- We need to specify a folder where we’re going to permanently install the DevKit. I recommend installing it in the root of your hard drive, at
C:\RubyDevKit. (Don’t use spaces in the directory name.)
Now we need to make the DevKit tools available to Ruby.
- In your command prompt, change to the DevKit directory.
cd C:\RubyDevKitor whatever directory you installed it in.
- We need to run a Ruby script to initialize the DevKit setup. Type
ruby dk.rb init. Now we’ll tell that same script to add the DevKit to our Ruby installation. Type
ruby dk.rb install.
The DevKit should now be available for your Ruby tools to use when installing new libraries.
Step 3: Rails
Now we can install Rails. Rails comes as a Ruby gem. In your command prompt, type:
gem install rails
Once you press Enter, the
gem program will download and install that version of the Rails gem, along with all the other gems Rails depends on.
Step 4: Node.js
- Download the Node.js installer from here.
- When the download completes, visit your downloads folder, and run the
- Read the full license agreement, accept the terms, and click Next through the rest of the wizard, leaving everything at the default.
- A window may pop up asking if you want to allow the app to make changes to your computer. Click “Yes”.
- When the installation is complete, you’ll need to restart your computer so Rails can access Node.js.
Once your computer restarts, don’t forget to go to the Windows menu, click “All Programs”, scroll down to Ruby, and click “Start Command Prompt with Ruby”.