Sofia Jonsson
Posted on September 29, 2019
This blog post is intended to be used as a refresher for basic Ruby on Rails Concepts. This might be helpful for someone who learned Rails a while ago and finds themselves asking "what was that one command again...?"
So, first off..
What is Ruby on Rails?
Ruby on Rails is a web-application framework that includes everything needed to create database-backed web applications according to the Model-View-Controller(MVC) and implementing a RESTful design. Rails has two main guiding philosophies: Don't Repeat Yourself (keep it DRY) and Convention over Configuration. What this means is that you want to keep your code as concise as possible. Writing lots of unnecessary code results in WET code (We Like Typing) which honestly nobody likes. Rails also has its own opinion over the best ways to do things in a web application and default to this set standard of conventions, instead of requiring you, the developer, to specify minutiae through countless configuration files.
Object Oriented Principles
In Ruby there are four principles of OOP:
- Abstraction: Keeping the viewed interface as simple as possible
- Polymorphism: Using the same method for multiple different objects. Modules and ActiveRecord(AR) are examples of this. They help your code be more dynamic and help you keep it DRY
- Inheritance: Models in Rails inherit from AR and provide us with helpful methods like find, sum, where, and average. Also helps keep code DRY!
- Encapsulation: Maintaining a barrier wherever possible. In Ruby we utilize Encapsulation when we only use attr_reader. This keeps the user of the application from being able to alter any of the code. When we incorporate attr_writer and attr_accessor we allow outside access. In Rails we use strong parameters to encapsulate and shield our attributes. Without strong params we are subject to outside hacking. By setting a private method like set_user and user_params we can limit @user to only be accessible within the scope of the show, edit, update, and destroy actions. The User will also only have access to changing their :username, :password, :bio, :photo_url. For a UsersController with a follow to follower relationship we would set up it up as so:
Creating a New Rails Project
First, make sure you have Rails installed on your machine, if you are reading this as a refresher, then it should already be there. You can check which version you have by running rails --v
and if its something like "Rails 6.0.0" then you're good to continue. To create a new application run the line rails new name_of_new_project
. Go ahead and open up your text editor, I personally use atom so I type in atom .
and then proceed to run the server in my terminal to see that my initial set up is working rails s
. You should be able to see this default image on http://localhost:3000
Generate MVC
The Model maintains the relationship between the objects and the databases through AR, and handles validation, association, traction, etc.
The View, ActionView, is what displays our code onto the webpage. The Controller, ActionController handles the commands for your Views. For instance, if you want to "show" your page, then you need to create a command for it in the controller.
This is where CRUD comes in. On an interactive web-page, you will often find yourself creating features for users to Create, Read, Update, and Destroy content. As an example, you can think about Instagram. If you were to create an Instagram copy site with Rails you would want users to be able to see their posts (Read), upload new content (Create), edit their captions (Update), and delete a comment or post (Destroy).
If you're in a hurry and you know which unnecessary files you want to remove for space complexity, then you can run the incredible rails g scaffold _name_ _attribute_:_type_
and Scaffold your models.
*g is short for generate (you can write it out if fully if you love typing)
However, if you want to do it manually, and were making an application about Movies then you would run the commands:
rails g model Movie name: string rating: integer
rails g controller Movies index show
rails g migration AddBiographyTo Movies biography:text
Note that the controller and migration are written plurally as the Model is singular.
At this point in your setup, you can run rails db:migrate
to run the migration and set up your relationships. Make sure your relationships between your models are set up the way you intended before you run the migration.
If you make a mistake and need to "go-back" then you can run rails db:rollback
and keep running it until you reach the point you want to continue from. This command is like the ctrl+z or cmd+z of your migrations but use it sparingly and go slow if you do end up needing it!
Associations are connections between models and make common operations much easier in your code. So for our Movie example, we could create another Model of "Directors" where both will have a relationship to AR and we can establish their relationship to each other through association.
The Associations between the models through AR use one of the six:
- belongs_to
- has_one
- has_many
- has_many :through
- has_one :through
- has_and_belongs_to_many
The belongs_to
association will be written in singular due to it belonging to one element. The plural has_many
pertains to the plural elements in the model. Ex:
Routes
Rails router organizes the URLs and expedites them to a controllers action (Rack application) and generates paths and URLs removing the need to hardcode strings within your Views.
When naming a route make sure to use the plural when defining a route for a resource. Ex: resurces :movies
If you are ever confused about what routes you have, you can always rake/run your routes in the terminal using to show the parameters of your routes.
A resourceful route in Rails maps between the HTTP verbs and the URLs to the controller actions. By convention, each of these actions map to a distinct CRUD operation in a database.
Ex:
I think that covers the basics. There are way more concepts and topics I could dive into, but I might just have to save them for a deep dive blog post!
Posted on September 29, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.