Follow Along as I Stumble on the Path to Learning RoR

Syntax

NoMethodError undefined method `[]’ for “image”:Sass::Script::Value::String

When upgrading my Rails app to 4 (from 3), I was getting the error:

NoMethodError undefined method `[]’ for “image”:Sass::Script::Value::String

Eventually I found out that the asset-path / asset-url methods changed to only accept one argument.

For example,

asset-url(‘profile.png’, image)

now should be:

asset-url(‘profile.png’)

In my particular case, this code was in some of my css files.


Returning 200 Status Code for Webhook

I was getting a 500 error code because when my webhook was getting triggered, the code was looking for a view or file to render when the processing was completed.

I tried: return 200

in my controller, but that didn’t work for me.

Instead the answer was:

head :ok


Simple_Form Custom Errors

I had some error messages not appearing in simple_form because the fields they were related to did not appear in the simple_form.  Normally, simple_form will highlight the field in the form that has a problem and display the error message next to it.  However, since the field related to the error wasn’t in the form, there was no message being shown.

To solve this, I had to add some additional code to display the text of the error message.

I fixed it by adding this to my simple_form forms:

<%= render 'shared/error_messages', object: f.object %>

and then created a partial, _error_messages (shared/_error_messages):

<% if object.errors.any? %>

<% end %>


Showing Errors in the Logs

I had some errors occurring in my controller, but it wasn’t displaying in my views.  So I had to figure out how to see what was happening since I didn’t see any messages in the logs that were helpful either.

I found the solution in one of the answers here on Stack Overflow.

By adding this to my controller (it was the creation of a model – @user):

logger.warn("====error==========#{@user.error.full_messages.inspect}============")

This recorded the error in my logs (one of my model validations needed to be changed).


Common Formatting Helpers

Adding  a list of some of the Rails formatting helpers I often have to use:

  • Converting a number to a dollar amount -> number_to_currency(field_value)
  • Changing the date to US date format (MM/DD/YYYY) -> field_value.strftime(“%m/%d/%Y”)
  • Converting to a percentage -> number_to_percentage(field_value)

For additional helpers with numbers here’s the link to the Rails API for number helpers


Getting Thin Gem to Work on Heroku When you Develop in Windows

Upgrading to Heroku’s Cedar stack, I had to install the Thin gem to prevent my app from running on Web Brick.  On the Bamboo stack this is injected so I need not worry, but with Cedar you need to install all the gems you need.

The problem is that I work on Windows in development and that one of the dependencies for the Thin gem is the Eventmachine gem.  However, Eventmachine isn’t really geared towards Windows environments (except gem install eventmachine –pre), so everytime went to run bundle install the install would fail because the dependency, eventmachine 0.12.10 could not install.

However, Heroku helped me find a solution.

Add this to the gemfile

group :production do
gem ‘thin’
end

and instead of ‘bundle install’, run ‘bundle install –without production’


Number Types in Rails

I mistakenly had some fields as integers, but they needed decimals.  I did some reading and found that people tended to favor :decimal over :float because :float numbers sometimes had errors.  Some people favored parsing  the numbers before the decimal, and the ones after and storing each as an integer.

I decided just to go with :decimal since most people recommended that.

For dollars (price), I used:

t.decimal :price, :precision => 16, :scale => 2

For sales tax, I used:

t.decimal :tax_percent, :precision => 6, :scale => 4

Precision is the amount of digits you can have in total, while scale indicates how many places you can have after the decimal point.  As someone pointed out, make sure that the precision number is more than the scale number.