Ruby on Rails for Beginners

Having started the new term at Coder Academy, I now have a basic grounding of Ruby. Now we go one step further and look at the nebulous framework of Rails.

Here’s what I’ve learned to get started.

Rails fundamentally follows a trifold structure. Essentially, (to get started), there is three elements: the router (upper management), the controller (middle management) and the view (the worker).

In internet terms, the homepage for twitter https://twitter.com is different from the homepage of my twitter https://twitter.com/NathanOtheDev, which is different from your homepage. You have to tell the router whose homepage you want to view, who then passes you to the correct controller (or user profile).

The view is where abouts within that profile you want to go. By default, no view explicitly stated might just mean go the the home view; or, you may want to look at a specific post, which would be like looking at:
https://twitter.com/NathanOtheDev/status/1313795792985964544

To start a rails app, these are the steps I’ve learned:
rails generate new <name> — creates a new rails project named <name>
cd <name> — change directory
bundle install — install gem dependencies
rails server — runs the server.

In those four lines, you have a local server up and running. To start developing your site, you will need the controller, routes and views.

Controller (Middle Management)
rails generate new controller <Name> — Creates a controller in app/controllers/Name_controller.rb
Within the controller file, add a function with the same name as what you will call the view (eg, home).

Routes (Upper Management)
Open config/routes.rb
Based on top-down priority; first matching expression will be run, so don’t have multiple matching names
get ‘url-slug’ => ‘controller#function’ — set a route to a page based on the url
root ‘controller#function’ — for setting the homepage

Views
Where the HTML/CSS/equivalent lives
Located in app/views/pages — Each file (page) should be within this directory

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store