When I started coding, I usually ignored reading the errors. It is mainly because of the abundant lines filled with letters in the terminal. Altogether, they look like a big mess, and they seem to be very difficult to comprehend.
When I started coding, I usually ignored reading the errors. It is mainly because of the abundant lines filled with letters in the terminal. Altogether, they look like a big mess, and they seem to be very difficult to comprehend. After a few weeks of continuously coding at Flatiron School, I realized that 99% of the time in coding is seeing those errors. Then, I know I should start to familiarize myself with them. To be completely honest, I don’t really like them. I see them as mundaneness that often makes me exhausted. Most of the time, I had to spend several hours just to fix a typing error. However, if I look at them more positively, the errors resemble as a passive instructor. Errors always have meaning, they give us hints to know the locations of our errors so we can find and fix them. I usually search on google to find a way to fix those errors by reading other people sharing on StackOverflow. Searching on Google does not always guarantee to give me the correct answer, but somehow I can still picturing 30–40% of what it is. And day by day, my ability to read errors has improved. Of course, the usefulness of those errors can never be compared to the real instructors’ guidance. Nevertheless, those errors will be one of my best friends in my coding life.
In this blog, I will show you some common errors that you often see when you learn to build a Rails application.
When a user has requested a URL: “http://localhost:3000/dogs”, they get this error.
- Our terminal would look like this:
- Our browser would look like this:
The most possible reason is our application lacks the route for “/dogs”. To fix it, you should go to the application’s “config/routes.rb” file, and add the routes properly. Our “routes.rb” file should look like this to help you pass this error.
In this case, if the URL is “http://localhost:3000/dogs/:id” and you got the same error. You might forget to make the route for “/dogs/:id” in our routes.rb file. If you want to understand more about Rails Routes, come here.
When a user has requested a URL : “http://localhost:3000/dogs/10”, they get this error.
This error is because our database doesn’t exist the dog with id = 10. There are several ways to check what dogs are in our database, but the easiest way is to go to our console to check. In the root directory of our application in the terminal, enter this command:
$ rails c
In our rails console, enter this command:
It will show all the dog id that you have in our database, likes the above picture.
For example, our migration and controller for dogs look like the pictures above. When we try to create a new dog with the name that exists in my database, it will show us this error
The error is because we set “unique” for our attribute “name” when we create the dogs table and model. It is not a worrying error since it follows our initial purpose to make the dog name unique. In this case, we should render the error to make it has a better display which a user can notice when they try to create the same name dog that we do not allow. Here is one of the solutions.
In this case, our “config/routes.rb” file looks like below
When a user has requested a URL : “http://localhost:3000/dogs”, they get this error.
Most errors name NoMethodError is because we haven’t defined that method anywhere in our application.
In this case, we don’t have “dog_path” method in our application. Why is that? It sounds weird because, as our experience, we didn’t see this error and the dog_path seems right since it likes the helper method URL for our dog show page.
It is because of other apps, we use “resources” in our routes.rb file. When we use “resources :dogs” instead of manually writing routes above, it will create automatical for us the URL helper path for index or show page as a conventional way. (likes dogs_path is the prefix of the dog index page, dog_path is the prefix of dog show page).
If you don’t want to use “resources :dogs”, another way is adding the prefix for our routes (like below):
By the 2 ways above, you successfully define “dog_path” method, so from now, you can use it anywhere you want.
5. NoMethodError: undefined method ‘’ for nil:NilClass
For example, we have 2 classes: Dog and Employee. Each employee has only one dog. Our employee model looks like the picture above. Usually, if we call “jim.dog” (jim is our employee instance), it will show us the dog object that Jim belongs to.
But when a user has requested a URL : “http://localhost:3000/employees/14”, they get this error. (14 is employee id of Jim)
Why it can be like that? Our dog class has the attribute “name”. So why can it be undefined method “name”?
The reason here is somehow we already deleted the dog that Jim belongs to. So “jim.dog” will be “nil”. Then we cannot call method “name” for a nil class. That is really tricky but reasonable.
These are 5 common errors that everyone often encounters when working on a Rails application. I hope this blog can help you overcome the fear of errors like I did.