Installation/ Creating a new Rails app/ Console / MVC/ Static and Dynamic Route

Rails is a web application framework running on the Ruby programming language. It allows you to write less code while accomplishing more.

Rails philosophy :

Before installing you have to make sure you have:

To install Rails use the gem install command provided by RubyGems:

gem install rails (rails — version)

Rails come with a number of scripts called generators that are designed to make development easier by creating everything that’s necessary to start working on a particular task. A new application generator provides the foundation of a fresh Rails app.

-rails new blog -> cd blog

The blog directory will have a number of generated files and folders that make up the structure of a Rails application. Most of the work in this tutorial will happen in the app folder, but here’s a basic rundown on the function of each of the files and folders that Rails create by default:

app/ — Contains the controllers, models, views, helpers, mailers, channels, jobs, and assets for your application. You’ll focus on this folder for the remainder of this guide.

bin/ — Contains the rails script that starts your app and can contain other scripts you use to set up, update, deploy, or run your application.

config/ — Contains configuration for your application’s routes, database, and more. This is covered in more detail in Configuring Rails Applications. — Rack configuration for Rack-based servers used to start the application. For more information about Rack, see the Rack website.

db/ — Contains your current database schema, as well as the database migrations.

Gemfile.lock — These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the Bundler website.

lib/ — Extended modules for your application.

log/ — Application log files.

package.json — This file allows you to specify what npm dependencies are needed for your Rails application. This file is used by Yarn. For more information about Yarn, see the Yarn website.

public/ — Contains static files and compiled assets. When your app is running, this directory will be exposed as-is.

Rakefile — This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application. — This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.

storage/ — Active Storage files for Disk Service. This is covered in Active Storage Overview.

test/ — Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications.

tmp/ — Temporary files (like cache and pid files).

vendor/ — A place for all third-party code. In a typical Rails application, this includes vendored gems.

.gitignore — This file tells git which files (or patterns) it should ignore. See GitHub — Ignoring files for more info about ignoring files.

.ruby-version — This file contains the default Ruby version.

In the blog directory:

-rails server
- rails s

This will start up Puma a web server distributed with Rails by default. http://localhost:3000

NOTE: Changes you make in files will be automatically picked up by the server.

You may have created a database before starting up the Rails server.
- rake db:create

Rails console it’s an important tool. Gives you a direct connection to your applications ecosystem and lets you perform tasks such as:

  • Running database queries
  • Running app code
  • Performing full CRUD tasks with the database
  • Allowing to switch between making permanent database changes and running in a sandbox mode to test scripts out.

To start: rails c
To close: ctrl+ d

The creators of Rails utilized MVC architecture. It’s a design pattern that divides the responsibilities of an app to make it easier to reason about.

Model — this is where you can be very expressive with custom algorithms that you want to utilize you also have direct access to the specific database record. The logic in the model files.

Controller — transmit date requests from the user to the model and then delivers data that is rendered in the view to the user.

View — view should not contain any programmatic logic, they should simply render what the controller sends it.

Routes are rules written in a Ruby DSL (Domain-Specific Language). Controllers are Ruby classes, and their public methods are actions. And views are templates, usually written in a mixture of HTML and Ruby.

Rails have a comprehensive routing system for both dynamic and static pages.

  • Static route — A static route will render a view that does not change. Typically, you will not send parameters to it. Examples would be a site’s about or contact pages.
  • Dynamic route — Dynamic routes are pages that accept parameters and render different content based on those parameters. An example would be a blog’s post page that contains a specific article.