Hi Coders! I’m here to tell you some things about Rails and why you should learn about it. So, what is Ruby on Rails?
Well, Rails is a web application development framework written in the Ruby programming language. They say it was designed to make programming web applications easier, which I do agree with. Rails allows you to write less code while accomplishing more than many other languages and frameworks.
Have you heard of about the principle DRY (Don’t Repeat Yourself), well Rails includes this major guiding principle and the Convention Over Configuration principle.
DRY- is a principle that states to not write the same information over and over again to make your code more extensible, maintainable and not as buggy.
Convention Over Configuration - Rails has opinions about the best way to do many things in a web application, and defaults to this set of conventions, rather than require that you specify minutiae through endless configuration files.
If this information so far has got you interested in creating a rails project, here are some tips to get started:
There are some prerequisites that you should have installed prior to installing Rails such as Ruby and SQLite. First open up your terminal and see if you already have Ruby installed on your machine and use this command:
You should have Ruby version 2.7.0 or later installed. Then check for SQLite with this command: sqlite3 --version. If that is installed then you are ready to install Rails. You install Rails by using this command: gem install rails
Then to verify it is installed: rails --version. Then you are ready to create your first Rails project, now run: rails new name_of_project. This command will create a new Rails project and install the necessary gem dependencies and generate a number of files and folders that make up the structure of a Rails application. Feel free to go ahead and look at the those at your own leisure.
If you want to start up the web server use this command: rails server
To stop server: Ctrl + C
As always, we’ll start by showing you how to say Hello in Rails
You should start by creating a route, a controller that has an action and a view. A route maps a request to a controller action. A controller action performs the necessary work to handle the request, and prepares any data for the view. A view displays data in a desired format.
To add a route go to the config folder and then the routes.rb file. You should seed something like this:
get "/names", to: "names#index"
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
The route above declares that Get /names requests are mapped to the index action of NamesController.
Then to create the NamesController and the index action, run: rails generate controller Names index --skip-routes
Rails will then create several files and the most important of them is the controller file, in app/controllers/names_controller.rb. It should look like this:
class NamesController < ApplicationController
Next open up app/views/names/index.html.erb and for the contents you want to replace it with: <h1>Hello!</h1> and restart the server.
MVC (Model-View-Controller) is a design pattern that divides the responsibilities of an application to make it easier to reason about. Rails follows this design pattern by convention.
We already have the controller and view, so now let’s generate a model. What is a model? you may ask, well it is Ruby class that is used to represent data. Additionally, models can interact with the application’s database through a feature of Rails called Active Record. To generate a model run: rails g model Name name:string body:text
g is short for generate
There are then several more files created and the two files that we’ll be looking at are db/migrate/<timestamp>_create_names.rb and app/models/name.rb.
Migrations are used to alter the structure of an application’s database. In Rails applications, migrations are written in Ruby so that they can be database-agnostic. The migration file should look like this:
class CreateNames < ActiveRecord::Migration[7.0]
create_table :articles do |t|
Then you’ll want to run rails db:migrate
Now if you’d like to create some data, navigate to the seeds file: db/seeds.rb. You can now do
Name.create(name: “Sarah”, body: “Hi my name is Sarah!”)
Then run rails db:seed
There are tons you can do with Rails and more information to cover but for now if you’d like a part 2 to this blog please like and follow for more!
Till next time, happy coding!