DEV Community

Alyssa Falcione
Alyssa Falcione

Posted on

Debugging common errors when coding in Ruby on rails

Debugging is a large part of being a software engineer, and it may be one of the more frustrating aspects of our job. But worry no more! I have put together this short blog on common errors you may encounter while coding in Ruby on rails, and how to debug them.

Let's first talk about the "puts debugging" technique first. Puts in Ruby on Rails can be used for many instances in debugging, but one of the more common use cases would be to find the value of a variable. Let's take a look at the example below:

puts variableValue
Enter fullscreen mode Exit fullscreen mode

The puts statement above will give us the value of variableValue. However, it may not tell us everything we may need to know, so there are a number of variations we can use to help us gain more information about variableValue. Such as:

puts variableValue.class  <-Will tell us what type of object we have

puts variableValue.inspect <-Will return a string representation of the object
Enter fullscreen mode Exit fullscreen mode

So therefore, "Puts" can aid us in debugging by evaluating the values of variables.

ActionController::RoutingError*

Routing errors are one of the more common errors you may run into. This happens when a URL has been requested that does not exist.

ActionController::RoutingError (No route matches [GET]"/flatIron"):
Enter fullscreen mode Exit fullscreen mode

When encountering this error, the first thing you should examine would be your routes.rb file to ensure that your route exists. If that is set up correctly, your error could likely be found in the associated controller dealing with the "Show" method.

NoMethodError: undefined method

NoMethodError: undefined method `your_method` 
Enter fullscreen mode Exit fullscreen mode

This error can happen for a few different reasons. One of the more common reasons would be due to trying to call a method on an object that doesn't exist or Ruby is unable to find. You can usually resolve this error by checking your controllers to be sure that 'your_method' exists and is set up properly.

NoMethodError: undefined method for nil:NilClass
Enter fullscreen mode Exit fullscreen mode

This error usually occurs when you pass an unexpected input to a class or method. You could also be receiving this error for trying to call on an object that has not yet been defined. Be sure to examine your methods to ensure the method and/ or associated object has been declared.

Unpermitted parameter:

Luckily with Rails 7, it now helps to provide context when we receive the "unpermitted parameter" error. Previous versions of Rails would not directly tell us the associated controller that the error is causing, but we now have that magic to make our lives easier. So when you see this error, head to your targeted controller to be sure that you passed all of permitted parameters properly. It is also important to check your schema to be sure that your table also has those a column associated with the parameter that is erroring out.

create_table "users", force: :cascade do |t|
    t.string   "firstname"
    t.string   "lastname"
    t.string   "email"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "password_digest"
    t.string   "password"
  end
Enter fullscreen mode Exit fullscreen mode

If necessary, create a migration to update your table:

$ bin/rails generate migration AddColumnToTable
Enter fullscreen mode Exit fullscreen mode

Then head to your migrations and add the column:

class AddColumnToTable < ActiveRecord::Migration[7.0]
  def change
   add_column :tableName, :columnName, :dateType
  end
end
Enter fullscreen mode Exit fullscreen mode

Be sure to run "db:migrate" in your terminal to update your schema after updating your migration.

*Lastly lets talk about CORS Policy *

One of the more frustrating errors I encountered as a beginner working with Ruby on Rails was the "Cross-Origin Request Blocked" error, which was mainly due to inexperience. Most often times to solve this you just need to add the ruby gem 'rack-cors' to your gemfile and be sure to run 'bundle install' after. You also want to be sure to add the proper configuration to your application.rb file, it should look like this:

  class Application < Rails::Application

    # ...

    config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Lastly, be sure to also add the configuration to your config.ru file:

require 'rack/cors'
use Rack::Cors do

 # allow all origins in development
 allow do
   origins '*'
   resource '*',
       :headers => :any,
       :methods => [:get, :post, :delete, :put, :options]
 end
end
Enter fullscreen mode Exit fullscreen mode

Some words of encouragement

During my project build for FlatIron Phase 4, I encountered every single one of these errors and learned a lot from debugging each and everyone of them. It is easy to become overwhelmed and frustrated when debugging errors in our code, but we must remember that it helps to give us the skills and experience to be more efficient in our coding in the future. Learning from our mistakes is a big part of being a Software Engineer so embrace the errors and see it as a learning experience. I hope that this blog is able to help beginners become more familiar with common Ruby errors and how to handle them with a positive outlook.

Top comments (0)