Base to Ace - Ruby on Rails 7 (Part 2)
I made it to part 2!!! š„³ļøš„³ļø I'm going to be writing a lot more now (when I'm not busy baking stuff)
Alright, Iām being productive now, and that is good. Welcome to part 2 where I do an example project to work out the basic workflow of Rails. Here is the link to part one if you havenāt read it.
Step 2: Learning about controllers and views
Following the steps in the LinkedIn learning course, I started creating a basic task manager. Some things that I learned:
How to write routes and the files required for those routes.
That using
renderin a view function can render the HTML file that you want to render, Rails will render the default function name file otherwise.Redirects are used to prevent forms from being submitted again.
Most controller actions either render a template or redirect.
You use tags <% %> for rendering just like in Django. And just as in Django when you loop in the HTML file, you need to have a different set of tags for the iterator and the values within.
To separate variables defined in HTML from those defined in the view, @ signs are added to variables rendered from the view.
I got a little confused in terms of parameters used in queries, but pulled through when I understood the structure. For a parameter to work it must be defined in the view of that webpage.
It is actually very simple. I wish Iād started out in Django from the web template backwards now, honestly. It makes everything make more sense.
Step 3: Modeling and Migration
Migration of a data model in Ruby is similar to that in Django. Just about applying the migrations.
However, the way that models are written are different, in Django youād write them as:
class Sample(models.Model):attr = <some_attribute>But in Rails, you could write it as:
class Sample < ActiveRecord::Migrationdef upcreate_table :samples do |t|t.string :attrendenddef downdrop_table :samplesendendHere, up will create the table, and down will delete the same table. There is a way to do both at the same time:
class Sample < ActiveRecord::Migrationdef changecreate_table :samples do |t|t.string :attrendendThis code allows for creation and deletion in the same line. You can also use the following command in the command line to do the same thing:
rails generate model Sample attr:stringThis will generate a model and a migration for that model. Now, to perform these migrations we can simply run
rails db:migrate.
Step 4: CRUD
Rails has a query system similar to Django (Iām starting to smell a connection here), in Rails it is called ActiveRelation, and it allows you to use Rails to query the database in a simpler way.
There is also a console similar to Django shell.
Okay, so the CRUD is quite simple and almost one-to-one with Django.
Establishing relations between tables is also the same except I believe that Rails actually does add a foreign key while Django adds a sort of faux foreign key (look it up, itās the oldest issue in the Django repo)
When youāre making CRUD routes, you can define your routes in your view and then have them handled at the top level in routes. Exactly like Django.
Iām sorry that Iām using Django so much, but it is my primary frame of reference for this. And they are very similar.
The CRUD however, is also broken down into two steps for each one of create, retrieve, update and delete. Thatās quite useful.
Rails cheats on using PATCH, weird. I can see the logic behind it to an extent, but still weird.
Conclusion
This is the end of the beginning for this series. And just 2 parts in. But now, I am going to be moving into the good parts: the actual projects. Iāve got the handle of this now, and I have GoRails, so we are about to ramp up our Rails journey.

