Posts

[ToDoList] Front-end Basics

Image
So we now know how to get Rails to perform all of the CRUD actions required to run an application with user data from the console. The next step will be to make this possible from the browser, and allow Todo items to be created, read, updated and deleted from our lovely website! The show View Creating Entries in the Front-End Front-End Feedback     We already have our basic database and our model set up from our efforts in the last section, so we only need to configure some routes and create a controller & some views to make our dream of front-end database manipulation possible. Let's start just by showing a Todo entry on a page on the site. We will need to utilise a bit more magic here, specifically in the form of param (eter) s - these are Ruby Hash objects that contain data to be passed between various components of the Rails application. In this case we will be

New Rails Apps with Docker Compose

Image
Following on from the Docker & docker-compose guides, it is possible to use docker-compose to build a Rails application that uses a PostgreSQL database without even installing Ruby on your local machine by instead using containers ! This becomes a very useful option to have if you work from many different workstations and don't want to ensure they are all equipped with the correct Ruby and Rails dependencies, or if you wish to use a version of Ruby / Rails not supported by your operating system (for example, Ubuntu 20.04 only allows provides up to Ruby 2.7 in its apt cache). In fact, Rails 7 added native support for Docker, such that it generates almost all of the default files we need for us! This comes with its problems and, I won't lie, updating this post to work with Rails 7 (it was previously written for Rails 6) has been a colossal pain and features a bit of hackery. However it is ultimately a good thing, and having

[ToDoList] Data FUNdamentals

Creating & Reading Entries Updating & Destroying Entries Entry Validation (feat. Error Objects) Further Reading     In order to allow Rails to interact with the database (DB) tables to create and manipulate entries in our database, we need to introduce the M in MVC: models . Similarly to controllers, these are Ruby classes that inherit functionality from a core Rails element - in this case we are inheriting from ActiveRecord (which is generally abstracted to ApplicationRecord in more recent versions of Rails). Because of this, similarly again to our controllers, most of what we need to do here is simply define the model - so long as it is valid and it follows the required convention, Rails will be able to figure the rest out itself! Remembering that models must be named for the singular of the table we migrated, let’s create our model in app/models/todo.rb : app/models/todo.rb c

[ToDoList] Setting up the Database

We now have something resembling a website, with pages to display and links to get to them. But this is supposed to be a To-Do List application, which isn’t quite the case just yet. Let’s change that by creating means to handle data. As briefly hinted at before in the Rails Application Overview , Rails manipulates data primarily using actions within the CRUD framework ( C reate| R ead| U pdate| D elete). These actions are run against Ruby objects that represent individual entries inside our application's database (DB) tables, which are then governed in our application by models . In this section we will look at another way in which Rails uses convention to streamline its configuration, and how we can use this to carry out the first step in getting our application to handle data: creating a database table to store our creatively named Todo entries. More Rails Conventions Initial Database Migrati

[ToDoList] Basic Pages

Image
Well that was fun. However all we’ve really done is spooked someone called Bob, without actually creating anything remotely resembling a web application. Let’s rectify this and create some pages one would normally find on a website; a Home page and an About page. Home & Away About Helpful Links [Insert Beatles Reference]     Because we already learned a bit about MVC, we know that we will need a controller and at least one view to display these pages (we won’t need to worry about models until we’re handling real data). As before, we’ll need to add a route configuration so Rails knows where to go, too. Rails provides a fair bit of magic to streamline this process in the form of generators . However, because we’re learning, we’re going to do this manually for now and learn what this magic entails, so we can appreciate it more later. We already created a PagesController in the last sec

Popular posts from this blog

New Rails Apps with Docker Compose

[ToDoList] Basic Pages

[ToDoList] Docker Compose