Web applications are a constantly evolving world and so are their tools. After working with C++ and going through all the principles of versioning with Git and GitHub, I thought it would be a good time to take on full-stack web development with Ruby on Rails.
Ruby on Rails, commonly known as RoR or just Rails, is an open-source web application framework that uses the Ruby programming language. In other words, it is a set of tools and conventions that makes building web applications easier. It saves you from writing a lot of code from scratch; rather, Rails gives you a structure with pre-built components that you can use.
Rails is one of the best frameworks for developing e-commerce sites. It provides many tools and conventions one usually needs while working with databases, user accounts, orders, and other features typical of e-commerce applications.
I am currently learning Rails from scratch with the goal of creating a fully functional e-commerce website. I am using a combination of resources, such as the official Ruby on Rails guides, helpful tutorials on YouTube, and modern CSS frameworks such as Tailwind CSS and Bulma.
Model View Controller (MVC) helped me logically structure the entire project while learning code in Rails and setting up an e-shop. Models represented store data, such as products with their names, descriptions, prices, product categories. Views set up the format through which the data is displayed to a user in their browser whether as product listing, product detail. Controllers then became intermediaries between models and views. This ensured smooth flow through the application and separation of individual logical layers.
It’s important to note that this web application is only active while the Rails server runs. The server, started with the rails server
(or rails s
) command in the terminal, acts as the engine that processes requests and serves the web pages. Closing the terminal or stopping the server makes the application inaccessible.
When installing Rails on Windows, I met a number of the typical pitfalls that come from this OS. That was why I decided to settle into the cleanest and least painful route due to WSL2-or Windows Subsystem for Linux 2.
WSL2 provided me with a fully-fledged Linux environment right on Windows. So, the installation and subsequent work with Rails went absolutely smoothly, as they would on a native Linux or even macOS system. I recommend this to anyone trying to save themselves from hassle and waste of time.
The MVC implementation inside Rails for products is shown in this resulting image, related to the routes.rb file. The resources – products route automatically generates paths for the various actions within the products themselves. These get displayed by a browser’s listing products as the output of that index action from the ProductsController controller, which relates to the Product model, that retrieves data from the database. The View then formats this into HTML and displays it for the user.
The Show this product and New product buttons point to other controller actions dealing with product detail and the form to create a new product, thus showing the connection between the individual MVC layers.
Implementing MVC in Rails for products is just the beginning, I would like to explore image optimisation, shopping cart management, payments and user accounts. My goal is to create a robust and user-friendly e-shop that will be fully functional.
Reflection
Building a fictional e-commerce store using Rails was intensive, but it introduced me to a lot of web development concepts. I got a foundation in CSS thanks to the Rails Asset Pipeline library and the efficient styling provided by frameworks like Bulma and Tailwind CSS.
Setting up a development environment with VS Code specifically for Rails greatly simplified my process, making the coding process more enjoyable and productive. I learnt the importance of thorough documentation, relying on the official Ruby on Rails guide and supplementing it with practical examples from YouTube tutorials.
I encountered problems when integrating the Trix editor with Active Storage and working with a SQLite database, especially when importing CSV data. These obstacles forced me to delve deeper into the documentation and look for solutions in Ruby on Rails guides. I learnt how to troubleshoot effectively, which are skills that will be invaluable in future projects.
Through this project, I gained a solid understanding of MVC architecture, database interactions, and the importance of data backup. This experience has equipped me with the skills and confidence to take on more complex web development projects in the future.