• Brooke Bachman

Common Rails Server Errors with MVC

In my coding Bootcamp we have been tackling Rails which includes creating applications, using RESTful routes and the MVC (Model, View, Controller).

I am so thankful to have the server as a tool to troubleshoot my code.

Coding without seeing what my output was, was disheartening, especially if I encountered a tricky error.

While building my application I have run into the same errors over and over.

So I am going to guide you through resolving very common errors in Rails.

The first error we will look at is this.

NoMethodError in Coaches#Index

From a high level this error message is telling us that the Coaches class does not have a "dog" method.

This error message also tells us that this error is in line 6, in our Index , Viewer.

Okay so what does all of this mean?

Our Index page is a collection of all of our coaches, and our goal is to display the Coach's bio, the diet that is associated with them and that particular diet's description.

In line 6, you can see I am calling a dog method on the coach instance.

This error is telling me that I do not have a dog method in my Coach class.

When we look at my Coach Controller, it is clear that I do not have a dog method.

Let's delete this dog method and see what happens.

Here is the result of deleting the dog method.

My Page is showing up exactly as I want it!

When we are iterating over this array of coaches. We are trying to go through this array and display each individual coach. When I corrected the dog method I was calling, my webpage fixed itself.

Lett's look at a few more common errors.

No route matches [GET] '/diets'

A get request is where the client, requests a certain url from the server.

This message says that when we typed "localhost:3000/diets"

we asked the server for the diets page.

In order for our server to answer our request we need a route for diets.

There are two ways to write this route however I am writing it the easiest way which is called resource routing.

Resource routing allows you to quickly add all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

If I want to have dry code I will implement a resource router and also use the only.

resources :diets, only: [:index].

When we only do resources :diets, we are creating all 7 restful routes for the Diets class.

By simply adding only: I am creating only the routes that I need for my App, and no extra ones.

The rails error messages are extremely helpful, and I have gained so much appreciation for how clear they are. Troubleshooting your rails application is an absolute joy, and everyone should appreciate how user-friendly it is.