My first rails app
Well well well, It was that time. After many years of developing PHP applications, I finally got the change to try Ruby. For a school project (Play.CMD) I got to work with Ruby on Rails. It was my first time using this for me new framework and I kinda like working with it. Even so much that I wanted to write about it. So let’s start with the project itself and why we decided to use Rails (Spoiler: it was not that spectacular).
Play.CMD is a community platform where students can post their work on and get feedback from other students based on their matching skills. By itself it sounds like an easy platform to make, create a post migration, add users to that post en render everything in a fancy wrapper. But no, it does have some complicated elements. To start with the post itself, because the post not only has some text in it. No it also has images, tags linked feedback and many more attributes linked to it. So with that in mind, we decided to not use Elixir, as we did last time. But go for a much simpler approach and use Rails. Mostly because Tjerk already did some applications with it and he has to do support work so it was the best solution.
So the decision was made, I had to work with rails for the first time in my life and it went… quite well. Because of the framework, I didn’t have to now that much about Ruby. And with the goal in mind, I could just easily make the elements we had to make.
Because of the knowledge I have from Laravel, it was easy to get into Rails. I was already familiar with things like Controllers and Models. So I only had to dive in to the syntax. To get familiar with the syntax, I dived in to some open source repositories to try and understand what happens on the code side. I found the repository
Forem which is the engine Dev.to runs on. In that repository you can find some quite complex code. Reading the lines and thinking about what it does helps me understand what happens. So using that tactic, I slowly but steadily started to understand Rails and the syntax. The result of it can be seen on the public repository of Play.CMD
Okay, so not everything succeed right away. I had some struggle with the template engine Rails uses. In my Laravel thinking box, I mostly use
@foreach() so to go from that to
<%= if () %> and
<%= element.each do |item| %> can be a switch to get familiar with. The many errors I got from the template engine helped me in better understanding how it works. And in a way I can already see some links between blade and erb. After getting used to the new syntax, I slowly started to do some refactoring. Separating files in partials, using yields to pass data back and forth. It all started to get logical to me. That also helped me in fixing some, at the start, for me issues like adding Meta data in a specific page for Social media. In my PHP experience, I knew that I had to use some kind of slot system to link data in the post file to the layout file. And with the knowledge I now have about Rails, I did find a way to even fix the issue.
<%= yield :head %> <%# Place this in the layout HEAD element %> <% content_for :head do %> <title>Play.CMD | <%= @post.title %></title> <% end %> <%# Place this at the top of the post show file %>
The above code demonstrates how I implemented the media meta data of specific pages to the layout. At the start of this semester I didn’t even know what
<%= %> did. Crazy huh.
It’s always refreshing to use a new dev environment. Last time (when I used Elixir) I got to the point that I loved Laravel. And now, I enjoy working with Rails because of the different technologies and components I can use. Every framework has it’s own pros and cons and after using them, you can make a good decision which to use. That may be the best outcome of this project for me. And of course the students that use the application helps me stay motived.