- Read Tutorial
- Watch Guide Video
In this lesson, we are going to learn how to create a simple Ruby on Rails program. Rails is a popular framework used by many Ruby developers, and I personally have the majority of my Ruby applications built on it. As opposed to the Sinatra framework, which prides itself on being lightweight, Rails has a large number of conventions that you'll need to learn in order to build full featured applications. In order to learn those conventions and how to build rails apps you can go through the courses: Learn Ruby on Rails from Scratch and Professional Rails Development.
Before going into the code example, ensure you have Rails
installed on your system. Like the Sinatra framework, Rails is a Ruby gem, and there are quite a few tutorials that show you how to install Rails, if you need a reference, here is a guide for installing and configuring Rails.
To create a new project type:
rails new basic-project
This command will build a large number of files built for you such as your config
files, tests
, assets
and more. Also, it integrates the necessary Ruby gems like json
, rake
and minitest
. It even establishes your database connections.
Next, let's create our database file and start the rails
server.
rake db:create rake db:migrate rails s
When the server starts, it shows what kind of server its using and the port.
Now, if we go to localhost:3000
, you can see the welcome page.
Let's go back to our console and create a scaffold with the code:
rails g scaffold Article title:string body:text
This command will create more files for us such as view
template files, a model
, controller
, database connections and just about everything else needed for our application.
Also, let's migrate our database because the previous command made more changes to it. To migrate, run the command:
rake db:migrate
If you go the application you can see all the files and folders it created for us. If you open the schema.rb
file, you can also see that it created a database table for us called articles
with title
, content
, created_at
and updated_at
parameters.
# db/schema.rb ActiveRecord::Schema.define(version: 20160114180152) do create_table "articles", force: :cascade do |t| t.string "title" t.text "body" t.datetime "created_at", null: false t.datetime "updated_at", null: false end end
It also created view forms for us that you can see in the _form.html.erb
file.
<!-- app/views/articles/_form.html.erb --> <%= form_for(@article) do |f| %> <% if @article.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2> <ul> <% @article.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %><br> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :body %><br> <%= f.text_area :body %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
Let's see if this all works. Start the rails server with the command rails s
. Go to localhost:3000/articles
and it will display this page:
If you click on New Post, it will take you to a page where you can enter the title and content.
We've been able to create all this functionality without writing a single line of code. Rails is massively more complex than our short introduction and has libraries of books written on how to build Rails applications. However this should show you some of the power of the framework and give you ideas for the types of applications you can develop.