+
Tips for Ruby Web Development - How to Build a Website With Ruby on Rails Framework?
Published at November 10, 2017
ruby_on_rails_web_development_laptop.jpg

Mobile apps bring significant benefits to both entrepreneurs and consumers. Recently, many people were asking us "How do you create an app from scratch, " and we usually point them to our guides for web and mobile app development. Since building an app from scratch can be a tiresome process, we always advise that you use a predefined code framework. We have also mentioned these structures in our web development guide.                                      

Ruby on Rails is one of the most popular frameworks for app development. It is a server-side model-view-controller framework, which gives us a predefined structure for databases, web pages and different types of web services. Ruby on Rails web development uses Ruby as the main programming language.  In this guide, we will teach you the basics of Ruby web development and provide you with a step-by-step guide for creating a web app in this environment.

We will create a straightforward app as an example, but Ruby on Rails web development also allows you to create complex web apps. We will start with the basic installation process:

How to install Ruby on Rails web development framework 

Some operating systems have a default Ruby installation. If you don't have one of these, you will need to go through a short installation process before you start working on your first Ruby web development project. You need to install Ruby if you work on Windows OS. In that case, you can find the installation files and guide on this link.

If you work on Linux or MacOSX, you probably have Ruby installed. Still, most of these installations are old, and the gems are difficult to manage. If this is the case with your OS, you will need to install Ruby manually.  The MacOSX installation is described on this link, where you will also find all the necessary files. Linux installation process is explained here.

Apart from installing Ruby, you also need to download and set up the gems. Bundler is a gem that helps you to build development dependencies while working on your ruby project. Apart from that you also need to install the Rail gem. 

If you work on Linux, you can install Bundler with “gem install bundler” command, while the latest Rails framework can be installed with “gem install rails -v4.0.0.rc2 “ command.

How do you create an app in Rails

Unlike most other developers, who create apps that list blog posts and todo lists., we have decided to add other types of functionalities to this Ruby on Rails web development project. The primary purpose of our app will be to manage and share bookmarks.

Rails framework comes with a large number of libraries and command lines. Rails is the most important one of all and you will use it to create the skeleton of your future app. Start with typing  “rails new mybookmarkapp -T -d sqlite3 -B”.

In this command, the ‘mybookmarkapp' is the name of our future app, ‘-T' stands for skipping Test::Units files because we won’t need to deal with these for creating a bookmark app. “-d sqlite" defines that we are going to use the SQLite type of database since Ruby on Rails offers several database options. Last, but not least, the "-B" command stops bundle installs from running.

After you have typed the command, you will see a long list of command lines that explain you the ongoing processes. The system informs you that the new "mybookmarkapp” directory is created and that it has a set of files and directories inside it.

Use of Bundler and Gemfile in Ruby web development

As we said earlier, Bundler manages the development dependancies in the Ruby app. It does that by reading the Gemfile, which is placed in the app’s root directory. So, to have everything running smoothly you need to install all the required gems. You can generate the Gemfile with the rails new command. After you ensure that you have all the default gems,  run the bundle install command we have skipped in the first paragraph of our guide.

Adding a bookmark resource to our Ruby on Rails web development project

Since our app manages bookmarks, we will first need to add the bookmark resource. To do that we will use the “rails generate” command. This way, we have created a scaffold that generates necessary files, and code to create an MVC stack for managing bookmarks. After that, we will shift to the database migration. As a part of it, we will also create the bookmark model. It will be used as a shell for manually issuing commands to the rails app. In there we can also create new bookmarks and place them into the table.

Apart from the Bookmark model, the scaffold generator has also created the Bookmarks.Controller and all other parts of the MVC stack. All the generated code can be tried on the server, even before you write a single line of code. That’s how great Ruby on Rails framework is.

Adding users and authentication with Ruby web development

After adding all the resources, you need to define the app's users and authenticate them. First, you need to install the Devise gem, and then generate the user model. After that we need to associate each user with all the bookmarks, they will be able to choose. In the end, we should add an authentication check to the BookmarksController that is automatically created by the scaffold command.

Handling wrong inputs

Wrong inputs can provide incomplete and malformed data when you are creating a new bookmark or request bookmarks that don't exist and cause an app error. You can do that by adding some validation rules to the Bookmark model. For example, you can validate the real bookmarks by defining title, length, users' ID, etc.

The simplest way of solving the problem of wrong and non-existent identifiers is by adding code lines to the BookmarkController to redirect the user to the Index page and notify them about an error.

Other ways you can improve the bookmark app

If you followed our guide, you created the basic structure of your bookmark app. Still, this is just a start of your development work, and many other tasks need to be done before the launch. You should improve your app in small steps, like:

  • Adding a better root page;
  • Adding a prettier GUI – you can use a predefined CSS framework for this one;
  • Create nicer forms – make use of the form gem;
  • Create a ’Welcome’ page – use SiteController#iindex;

You can find more information on creating a bookmark app in this elaborate guide written by Andrea Pavoni. If all this sounds too complicated and you are not experienced in back-end development, you should hire professionals for this task. Virtual Spirit provides back-end development services, using the Ruby on Rails framework. Just contact our development team, and they will help you to create a custom app that will engage your targeted audience and improve your marketing strategy.

Insights
Check Other Related Posts
man-wearing-black-polo-shirt-and-gray-pants-sitting-on-white-935977.jpg
This blog post gives you tips regarding hiring a Ruby on Rails developer.
December 25, 2019
NoPath@2x.jpg
Ruby / Rails libraries and frameworks for building realtime web applications
June 12, 2018
NoPath@2x.jpg
A list of SaaS products built with Ruby on Rails
May 02, 2018
View All Insights