Select Page

From so many coolest things, Ruby is one of them! It was invented in Japan in the 90’s. Ruby on Rails is clean and pure object-oriented language with flexible and comfortable for the developers coming from C and Perl.

But the best part is, it’s more than easy to learn and develop web apps. And since its last release of Ruby on Rails 5, much of developers’ attention being heaped on it.

In Ruby on Rails 5, it’s now possible to develop a web app in Ruby without having to use Rails. To do so, you just have to tell Apache to invoke your Ruby scripts via CGI.

Once you get your RoR (Ruby on Rails) on your system, the procedure of developing a web app is pretty simple. So let’s take a stroll through building your first simple web app in Ruby on Rails.

In this tutorial, we’ll develop a simple URL shortener web app in Ruby on Rails.

  • Developing First Ruby on Rails Application

Once you install Ruby on Rails, you need to generate a simple application.

First, open your command prompt – if you’re on windows, make sure you use “command prompt with Ruby on Rails” from the start menu to open it.

Now change the path of the directory where you want to save your web app code. In the directory, use the “rails new” command to create a new Ruby on Rails application.

To generate a new project, run following in your command prompt.

  • Rails new shortener (your project name)

  • Cd shortener

Once you generate your project, run bundle install command in your command prompt. This will basically tell the bundler to fetch and install all the libraries and your empty Rail application will be up and running.

  • Adding Feature

Once you have the empty Rails application, it’s time to start developing shortener. Since we’re developing a URL shortener web app, we’ll need a way to save a record of all URLs. and for this, we’ll create a URL model with a specified field: URL.

To generate the class, run the following command in your command prompt.

  • rails generate model URL (URL: string)

The above command will tell Rails to create a URL class for our model. Next, use your text editor to open the app/models/urls.rb file that consists your URL model and generates the migration file inside.

The migration should look like this:

class CreateUrls < ActiveRecord::Migration
def self.up
create_table
:urls do |t|
t
.string :url
t
.timestamps
end
end

def self.down
drop_table
:urls
end
end

And, your URL model should look like this:

class Url < ActiveRecord::Base
end

Now use your text editor to edit the Model. Enter the following code to edit your Model file

class Url < ActiveRecord::Base
validates
:url, :presence > true
end

NOTE: Don’t forget to save the file.

To test your Model, you’ll need to initialize a new URL instance with the following command:

u = Url.new

It will show something like below:

#<Url id: nil, url: nil, created_at: nil, updated_at: nil>

After initializing, save it to the database.

u.save

Since it is invalid, your console will output false. To fix it, you’ve to first create a new URL by passing in the option:

u = Url.new(:url => “https://google.com/”)

Now run u.save again. This time it will return true and it will be saved in database successfully.

Conclusion

Congratulations! Now you’ve successfully installed Ruby and developed your first Ruby web application.