What is Rails?
Rails is a web application development framework written in the Ruby programming language. It aims to simplify web application programming by assuming what every developer needs to get started. It allows you to write less code and do more than many other languages and frameworks. Experienced Rails developers also report that it makes web application development more fun.
Before installing Rails, ensure that the correct prerequisites are installed on your system. These include:
To Install Ruby
Open a command line prompt. On macOS, open Terminal.app; in Windows, choose Run from the Start menu and type cmd.exe.
Any command preceded by the dollar sign $ must be executed on the command line. Make sure you have the current version of Ruby installed:
ruby --version Ruby 2.7.0
To Install SQLite3
You also need to install the SQLite3 database. Many popular UNIX-like operating systems ship with acceptable versions of SQLite3.
Others can find installation instructions on the SQLite3 website.
Check that it is correctly installed and in your load path
To install Rails, use the gem install command provided by RubyGems: you should be able to run the following command in a new terminal :
Ruby on Rails Web Application
Create Blog Application
Create blog application scripts called generators Designed to make your development easier by creating everything you need to start working on a specific task. One of them is the new Application Builder, which will give you the basis for a new Rails application so you don’t have to write it yourself.
To use this generator, open a terminal, navigate to a directory where you have permission to create files and run:
Once the blog application is created, go to its directory
Generate the model
Models are Ruby classes used to represent data. Additionally, models can interact with the application’s database through a Rails feature called Active Record.
To define a model, we’ll use the model generator:
bin/rails generate model Article title:string body:text invoke active_record create db/migrate/_create_articles.rb create app/models/article rb invoke test_unit create test/models/article_test.rb create test/fixtures/articles.yml
migrations are used to modify the database structure of the application.
In Rails applications, migrations are written in Ruby, so they can be database independent.
Let’s see the contents of the new migration file:
class CreateArticles < ActiveRecord::Migration def change create_table:articles do t t.string:title t. text:body t.timestamps end end end
The call to create_table specifies how the articles table is constructed. By default, the create_table method adds an id column as an auto-incremented primary key. Therefore, the first record in the table has ID 1, the next record has ID 2, and so on.
Two columns are defined in the create_table block: title and body.
It was added by the generator because we included it in our build command
(bin/rails generate model Article title:string body:text).
The last line of the block
is a call to t.timestamps. This method defines two additional columns called created_by and updated_by. As we’ll see, Rails manages them for us and sets values when we create or update model objects.
Let’s run our migration with the following command:
This command will show the output that the table has been created:
== CreateArticles: migrating ========= == = ======================== -- create_table(:articles) -> 0.0018s == CreateArticles: Migration(0.0018s) == = ==========================
Ruby on Rails RVM
RVM stands for Ruby Version Manager. It’s a command-line tool that lets you easily install, manage, and collaborate with different Ruby environments. By using RVM, you can easily install different versions of Ruby and easily switch between them.
RVM is maintained by the GitHub community by sending pull requests to the project repository.
Syntax of RVM is:
rvm command_options command ruby_to_act_on
RVM flags allow you to change the behavior of RVM.
For a list of RVM commands, type the following: