Building a Web Application Without Rails : Serving Static Files

June 26, 2022   • no-rails-app

This is the second post in the series on building a web application without using Rails. In the last post, we set up a very basic project structure for our web application which returns a hard-coded string response directly from the Ruby code.

class App
  def call(env)
    headers = { 'Content-Type' => 'text/html' }

    return [200, headers, ['favicon']] if env['PATH_INFO'] == '/favicon.ico'

    response = ['<h1>Greetings from Rack!!</h1>']
    [200, headers, response]
  end
end

In this post, we will separate the view from the application logic, and serve static files using the Rack::Static middleware. If you prefer to watch a video instead, scroll to the bottom of this post.

Read More

The Rails Configuration File

June 22, 2022   • rails

Did you know that Rails has a ~/.railsrc file?

Similar to your ~/.bashrc or ~/.zshrc file, you can use this file to configure your Rails applications.

Rails Configuration File

This is especially useful if you find yourself repeatedly typing --skip or --no-skip commands and installing specific gems every time you create a new Rails application.

Since I switched to Rails from .NET last year, I must have created at least 20-30 projects for either learning, experiments, or client work. I have a text file that documents all the gems I want to install for a fresh Rails app. Every time I create a new project, I go through the file to install all the gems I want for my project.

With the ~/.railsrc file, I don’t need to do that. Rails will do it for me. Here’s how it works.

Read More

Building a Web Application Without Rails : Project Setup

June 18, 2022   • no-rails-app

This is the first post in the series on building a database-backed web application without using Rails. In this post, I will setup the project and create a very simple Rack application running on Puma app server. I will also show how to use Rack middleware to reload the application whenever the source code changes.

Ruby on Rails

I hope that in the process, we all will learn more about how web applications work and get a big-picture understanding of the web frameworks, especially Ruby on Rails. As a side-effect, we will learn a lot of meta-programming in Ruby.

Read More

Ruby Shortcut to Call a Method with Each Array Item as Parameter

June 10, 2022   • ruby

As a Ruby programmer, you probably know the &: shortcut to call a method on each item in an array. For example, this snippet will call the to_s method on :foo and :bar.

[:foo, :bar].each(&:to_s)

The above code is similar to:

[:foo, :bar].each do |item|
  item.to_s
end

However, instead of calling a method on the array item, you want to call a method and pass the array item to that method. Is there a one-liner for that?

[:foo, :bar].each do |item|
  puts(item)
end

It turns out, Ruby has a corresponding &method shortcut to accomplish precisely this.

Ruby Shortcut

Two things are going on here.

Read More

How to Easily Navigate Directories in the Terminal

June 6, 2022   • linux

You know about the PATH environment variable. Did you know about CDPATH? If you’ve been typing long cd commands like me, this post is for you.

I came across this gem this morning, while reading Daniel Barrett’s Efficient Linux at the Command Line. It instructs the cd command to search for the directory you specify in locations other than your current directory.

A cd search path works like your command search path $PATH. However, instead of finding commands, it finds the subdirectories. You can configure it with the shell variable CDPATH, and it has the same format as the PATH variable: a list of directories separated by colons.

Read More

How to Manage Environment Variables in Rails

June 2, 2022   • rails

Now you might think it’s so easy to access them using the ENV hash. Why do we need a different way?

Well, a very common problem when working on multiple Rails applications on the same machine during development is that these applications might have environment variables with the same name. A good example is the database connection string. Different applications will have a different value for the same environment variable. If you set the environment variable in your shell config file, how can you use different values?

The same problem will happen in a Continuous Integration (CI) server, where you might have multiple projects running.

So, don’t set the environment variables on the development machines or CI servers. Use the dotenv gem that lets you load variables from a .env file into the ENV hash when the Rails application loads.

Let’s see how to use this gem.

Read More

The Definitive Guide to Rack for Ruby and Rails Developers

May 21, 2022   • ruby rails

You’ve been around in the Rails world for a while. You know your way around rails. But you keep hearing this word ‘Rack’ and don’t really understand what it is or what it does for you. You try to read the documentation on the Rack Github repository or the Rails on Rack guides, but the only thing it does is add to the confusion.

Everyone keeps saying that it provides a minimal, modular, and adaptable interface for building web applications. But what the heck does that really mean?

Rack Logo

If there’s a list of topics that confuses most new Rails developers, Rack is definitely up there at the top. When I started learning Ruby and Rails last year, Rack took a really long time to wrap my head around. If you are in the same boat, fear not.

In this article, I will try to explain pretty much everything that you need to know about Rack as a Ruby and Rails developer. It covers everything from the basics to more advanced stuff such as middleware and writing your custom middleware. It’s quite a long article, but if you stick till the end, you will know more about Rack than pretty much everyone else.

Read More

Why You Need Strong Parameters in Rails

May 17, 2022   • rails

You’ve used the strong parameters in your Rails applications, but did you know what problem they are solving? I didn’t. So did some reading and learned about a common security vulnerability. In this post, I will explain the Mass Assignment vulnerability and how you can use the Rails strong parameters API to address it.

Read More

Understanding Rails Parameters

May 16, 2022   • rails

If you are building a dynamic web application, you will need to access the user-submitted data on the server. Maybe the user uploaded a photo, wrote a comment, posted a tweet, or clicked a link that includes the id of the article they want to read. How do you access this data on the back-end?

That’s where parameters enter into the picture. This article explains everything you need to know to understand Rails parameters.

Read More

When to Refactor Code

May 14, 2022

When you think of refactoring, you imagine it in the context of TDD. Before working on a feature, you first write a failing test. Then you make it pass by writing code that implements that feature. Finally, you refactor the working code to make it more readable, clean, and beautiful.

In our industry, it’s known as red-green-refactor. You start with red (failing test), and move to green (working code) code, ending with refactored code with a better design. However, there are some other times when refactoring code can be very useful. This post explores a few.

Read More
Page: 2 of 19