Another Ruby on Rails cheat sheet

This blog post is an experiment. I’ve shared some phrases I searched for below, with the abbreviated solution I was searching for. I’m hoping that this blog post will replace the other results.

rails test error stack trace

  • fixtures implicit conversion of Fixnum into String
  • rails TypeError: no implicit conversion of Fixnum into String

Solution: Fixture names should not be numeric

rails rake test stack trace

Rails.backtrace_cleaner.remove_silencers!

rails update_attributes

 my_model.update({ field1: ‘a’, field2: ‘b’})

html anchor disabled

  • javascript a click cancel
<a onclick=“e.preventDefault()”>

javascript array compact

 [1,2,3].filter(function(n) { return n > 2 })

undefined method column for Foreigner

foreigner is deprecated

rails parse integer with comma

 string.gsub(/\D/, ‘’).to_i …

That is, remove everything that isn’t a digit.

 s.gsub(/[^\d\.]/, '').to_f.round.to_i

find local path of gem

 gem environment

require actionview helpers

  • require view helpers
include ActionView::Helpers::NumberHelper

active record where less than

Post.where(“date < ?”, DateTime.now)