Fixing “Single arity template handlers are deprecated” error when upgrading from Rails 5.2 to Rails 6.0

DEPRECATION WARNING: Single arity template handlers are deprecated. Template handlers must
now accept two parameters, the view object and the source for the view object.
Change:
  >> Coffee::Rails::TemplateHandler.call(template)
To:
  >> Coffee::Rails::TemplateHandler.call(template, source)
 (called from at /Users/MediocreManta/Desktop/freedom_podcasting/Rakefile:6)

This was an error I encountered while upgrading my application from Rails 5.2 to Rails 6.0. I found the fix on this Github page, but I thought I’d write it out here to make it a bit clearer for the young programmers out there.

To fix it, I upgraded the coffee-rails by doing the following:

  • Open Gemfile
  • Change gem 'coffee-rails', '~> 4.2'` to `gem 'coffee-rails', '~> 5.0.0'
  • Stop server in terminal by pressing ctrl + s
  • Running the command bundle
  • Restarting the server with rails s

Hope this helps!

I’ve added some language to the Rails documentation via this PR. Hopefully the powers that be accept the addition so future generations can skip having to look this up.

Comma Separated List of Array – Good idea for Ruby on Rails?

I feel like there should be a rails function for doing a comma separated list from an array. Here’s how to do it explicitly:

<% search_tags.each_with_index do |tag, index| %>
  <% if index < search_tags.size - 1 %>
    <%= "#{tag}, " %>
  <% else %>
    <%= "#{tag}" %>
  <% end %>
<% end %>

…maybe `render_comma_separated_list(array)` or something.

An example array would like this = [“one”, “two”, “three”]

render_comma_separated_list(array)

=> one, two, three

Does something like this exist already?

Is this worth the effort of making a PR to Ruby on Rails?

Understanding Polymorphic Associations in Rails

When first trying to get an understanding of polymorphic associations in Rails, I was completely lost. At this point in my learning curve, polymorphic associations are a high-level concept in Rails which allows the programmer to reuse a single model to be useful for other models. It’s difficult to describe without an example. So… here we go.

An example of a polymorphic association is as follows. Let’s say you’re building a sports history database. Baseball players and basketball players are both athletes, but you want to be able to keep the baseballers separate from the basketballers. One way to do that is to create an Athletes model.

The Athlete model would be a way to tie the sports together.

Coding The Polymorphic Association

This requires a migration where we connect the athletes with the sports they play:

rails g migration AddSportToAthlete sport_id:integer sport_type:string

First we set the polymorphic association model:

class Athlete < ApplicationRecord
  belongs_to :sport, polymorphic: true
end

Then we assign that association to the baseballers and the basketballers:

class Baseballer < ApplicationRecord
  has_many :athletes, as: :sport
end

class Basketballer < ApplicationRecord
  has_many :athletes, as: :sport
end

Seeing a Rails App Develop From Looking at Git Changes

I’m deep diving into Ruby on Rails this week. One of the things I find interesting is how the scaffolding creates new parts of a Rails app. This is a helpful tool for understanding the stack so I’m stepping through it looking at the changes made which we can see better with git.

Below is a step by step view of the files created when developing a new Rails App. 

Stepping Through Rails and Watching Git Changes

rails new appName

  • git status => All code related to Ruby on Rails

appName => bundle update

  • git status => No changes

appName => bundle install

  • git status => No changes

appName => bin/rails generate controller name_of_controller

  • git status =>
    • app/assets/javascripts/ian.coffee
    • app/assets/stylesheets/ian.scss
    • app/controllers/ian_controller.rb
    • app/helpers/ian_helper.rb
    • test/controllers/ian_controller_test.rb

Note: git changes do NOT note that when generating a new controller, Rails also generates an empty views folder. This is an important thing to be aware of as your view templates will go here related to your new controller.

appName => bin/rails generate model name_of_model title:string body:text slug:string

  • git status =>
    • app/models/tree.rb
    • db/migrate/
    • test/fixtures/trees.yml
    • test/models/tree_test.rb

Creates the new files needed to maintain a model, it’s test data and test process.

appName => bin/rails db:migrate

  • git status =>
    • db/schema.rb

This process specifically reflects your new model’s functionality with the database.