Intro to Rails
This week I thought I’d give a little intro to ruby on rails. The past few weeks I’ve been getting back into rails for job interviews and I gotta say, I forgot just how easy rails can be to get an app up and running quickly. So the aim of this post is to get you up and running with your own rails project and run with it.
Okay so first things first Rails is a web application framework written in ruby. It allows you to write much less code to get the job done. Rails uses the DRY principle of coding, Don’t Repeat Yourself. This allows for cleaner looking code.
Before you install rails make sure you have the following installed first.
- Ruby
- SQLite3
- Node.js
- Yarn
Now we’re ready to install rails open up your terminal and type
gem install rails
once that is complete you can make sure it’s installed by typing
rails -v
and this will show you a version if it installed correctly. If anything else pops up just read through your terminal to see any errors and follow the advice given.
you should see something like this. I am running rails version 6.1.3.2.
Once that’s done you can start up a rails app with the following command.
rails new todo
were todo is whatever you want to name your app.
Once created make sure to cd into the new directory.
cd todo
now you can run rails s and open a web browser and go to localhost:3000
you should see the following.
Yay!! you’re up and running.
if you open your app in an editor like VSCode you will see a bunch of folders and files but don’t worry I’ll tell you the most important ones.
So looking at this screen shot we see app at the top, this is a big one. Most of what you’re working on will be in here. Other than that the config, db and possibly the gemfile are all you need to worry about.
Let's open up app and see what's in there.
So in here we will work with MVC models views controllers. A model is a 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. your views decide what renders to the webpage, and the controllers hold all the methods etc…
ok so lets get something on the screen.
to do this we will need to add a route to our config/routes.rb
Rails.application.routes.draw do
get "/todos", to: "todos#index"
end
now lets create a controller.
app/controllers/todos_controller.rb
this should contain…
class TodosController < ApplicationController
def index
end
end
now let's create app/views/todos/index.html.erb
and this should contain
<h1>Hello, Rails!</h1>
or anything you want inside the h1 tags.
now we can go back to our routes.rb file and add a root route, the first page that shows on start up.
Rails.application.routes.draw do
root "todos#index"get "/todos", to: "todos#index"
end
now visit localhost:3000 on your web browser and you will see a blank page with Hello, Rails! or whatever you decided to type.
yay! that's pretty much it. There is a lot you can do now. Rails has great documentation and there are wonderful tutorials online but this is your jumping off point.
Thanks for reading!!