From Sinatra To Rails

Vanessa Fotso
3 min readJul 21, 2020

--

Sinatra, being a small framework (Domain Specific Language) is a great place to start if you want to fully comprehend

what is going on behind the scenes when using rails fancy metaprogramming. Adapting the MVC model in Sinatra has

made the transition pretty smooth between the two frameworks. The main difference is that Rails give us multiple

boilerplate codes essential from front to back to work with compare to Sinatra, where we have to code every part of the

MVC from scratch. The downside is that Rails generators give us more boilerplate code than we need, and it might take a

good amount of time to clean up the unused files. However, we can exclude unnecessary boilerplates for our app by using

the options available after running ` rails new -help` . We will discuss how to

generate the MVC model and routes in Rail Vs Sinatra.

1. Routes Drawing

* Sinatra

class BlogApp < Sinatra::Base 
get "/posts" do
@posts = Post.all
erb :index
end
get "/posts/new" do
erb :new
end
post "/posts" do
Post.create(params[:post])
redirect '/posts'
end
get "/posts/:id" do |id|
@post = Post.find(id.to_i)
erb :show
end
get "/posts/:id/edit" do |id|
@post = Post.find(id.to_i)
erb :edit
end
put "/posts/:id" do |id|
@post = Post.find(id.to_i)
@post.update(params[:post])
redirect "/posts/#{id}"
end
delete "/posts/:id" do |id|
Post.delete(id.to_i)
redirect "/posts"
end

end

* Rails

Rails.application.routes.draw do 
resources :posts
end

Rails creates all seven RESTFUL routes with one single line of code and link those to their respective controller action. All is left to do is to add the logic and the view. It is important to know Rails naming convention as it emphasizes convention over configuration. This makes Rails efficient when it comes to separate concerns. Unlike Sinatra, routes are defined in a separate file and linked to controllers actions and view by naming convention. However, we can also tell rails explicitly to direct the request to a specific controller action or render a specific view. Also, Rails makes it easier to define nested routes.

Rails.application.routes.draw do
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
get 'signup', to: "users#new"
root 'static_pages#home'
get '/home', to: "static_pages#home"
resources :users
resources :books, only: [:show, :index, :destroy] do
resources :discussions, only: [:new, :show, :index]
end
resources :discussions, only: [:index, :new, :show, :create, :destroy]

end

Controller

class UsersController < ApplicationController
def new
# render "new"
end
end

2. Model View Controller

* Sinatra

As shown above, Sinatra gives no boiler plate to start with. The developer must code every single part of the MVC plus the related migrations. Plus, since the browser only recognize GET and POST, as HTTP verbs, we have to manually configure our Sinatra app to accept PUT and DELETE functionality:

class Server < Sinatra::Base 
set :method_override, true
#other things omitted
end

#Inside the form, include a hidden input, as follows
* (the value field specifies the verb to use):
<input type="hidden" name="_method" value="put">

* Rails

Rails provides generators that structure our directory for us by writing boilerplate code, so that we do not have to manually structure our app from scratch. Taking the User model for example, we can use:

rails generate model User name:string

this will generate a user model with the associated migration

rails generate controller Users new

this will generate the users controller with the new action. This will also creates a corresponding view file and helper for the view

If needed, we can also reverse a generator by running rails destroy like rails destroy controller .... This will delete all files the generator created, which is time efficient.

Last, we can also use rails scaffold to create a quick demo of the app. It automatically generate a simple set of a model, views and controller. This is great to use only to learn how Rails works by going over the code generated by the scaffold.

rails generate scaffold user name:string age:integer

Learning Sinatra first helped me to understand Rails magic. It was definitely the process of learning to walk first before being able to run.

Originally published at https://vanessuniq.github.io on July 21, 2020.

--

--

Vanessa Fotso
Vanessa Fotso

Written by Vanessa Fotso

Health IT Software Engineer with broad technical exposure and passion for learning. https://www.buymeacoffee.com/vanessuniq

No responses yet