Monthly Archives: February 2012

undefined method `merge’ for 5:Fixnum – Hidden Field

Just a quick post on an error I ran into today while using hidden fields:

undefined method `merge' for 5:Fixnum
 
27:   <div class="actions">
28:     <%= f.submit :class => 'button' %>
29:   </div>
30:   <%= f.hidden_field(:product_id, @upload.product_id) %>
31: <% end %>
32:

Thankfully this is an easy fix, simply amend your code as follows:

1
2
3
4
5
<div class="actions">
    <%= f.submit :class => 'button' %>
</div>
   <%= f.hidden_field :product_id, :value => @upload.product_id %>
<% end %>

And that’s all there is to it, let me know if there are any issues.

Not Recognised by the ‘identify’ command – Paperclip

Just a quick post on another problem I ran into while setting up PaperClip. When attempting to submit a form WITHOUT an image I received the following error message:

Assets asset /tmp/stream20120218-22611-dgnkr-0.exe is not recognized by the 'identify' command.

Apparently the most common cause of this issue is one of two things:

#1: You don’t have RMagick installed
The easiest fix for this is to simply install the rmagick gem. Simply add rmagick to your Gemfile and run bundle install. Restart your server and you should be ready to go.

#2: Your command_path isn’t set correctly
Run the following command from your console:

chris@chris-VirtualBox:~/site$ which identify
/usr/bin/identify

Then add the path that is returned to your environment settings:

1
Paperclip.options[:command_path] = "/usr/bin"

#3: If all else fails

If none of the above seem to be causing your issue, try adding the following to your model:

1
2
3
 def asset?
    !(asset_content_type =~ /^image.*/).nil?
  end

This will simply prevent the file being processed if it’s not an image, avoiding the issue all together. Hopefully this’ll help someone out, let me know if you come across any other solutions.

UPDATE:
Apparently a more recent cause of this issue is an update to the cocaine gem, rolling back to version 0.3.2 appears to resolve the issue. Thanks to Ahmed and Fabrice for bringing it to my attention:
https://github.com/thoughtbot/paperclip/issues/1038
http://stackoverflow.com/a/12760881/522859

PaperClip Issues – Ruby on Rails

I finally decided to replace all my existing code to handle images with Paperclip. I was following the screencast by Emerson Lackey, #134 Paperclip, however I ran into a couple of issues. Thankfully they were all very easily fixed, and probably wouldn’t have occurred at all if I’d simply watched the whole screencast instead of trying to rush on ahead.

Issue #1:
The first issue I encountered was that my asset fields weren’t appearing on the page:

1
2
3
4
5
6
7
<% f.fields_for :assets do |asset_fields|   %>
      <% if asset_fields.object.new_record? %>
        <p>
          <%= asset_fields.file_field :asset %>
        </p>
      <% end %>
    <% end %>

I spent way too much time trying to work this one out, especially considering how obvious the solution is – the screencast is simply missing an “=” after the initial “<%" in "<% f.fields_for". Simply amend it as follows:

1
2
3
4
5
6
7
<%= f.fields_for :assets do |asset_fields|   %>
      <% if asset_fields.object.new_record? %>
        <p>
          <%= asset_fields.file_field :asset %>
        </p>
      <% end %>
    <% end %>

Issue #2:
The second one is actually encountered and addressed by Emerson himself. Unfortunately I started trying to find a solution before seeing his, so just in case someone else does a Google search I’ll include the issue here:

undefined method `symbolize_keys!' for "/system/assets/1/original/mack_truck.jpg?1329550205":String

     <% f.fields_for :assets do |asset_fields| %>
       <% unless asset_fields.object.new_record? %>
       

<%= link_to image_tag(asset_fields.object.asset.url(:thumb), asset_fields.object.asset.url(:original)) %>

<% end %> <% end %>

The problem here is simply that the parenthesis are in the wrong place, simply amend your code as follows:

1
2
3
4
5
6
7
     <% f.fields_for :assets do |asset_fields| %>
       <% unless asset_fields.object.new_record? %>
       <p>
         <%= link_to image_tag(asset_fields.object.asset.url(:thumb)), asset_fields.object.asset.url(:original) %>
       </p>
       <% end %>
     <% end %>

Anyway, hopefully this helps someone else out – let me know if there are any issues.

Update: Don’t name your model assets!
Unfortunately I followed the tutorials naming example and called my model Asset. While this may have been fine in earlier version of rails it causes quirky conflicts with the asset pipeline. I strongly encourage you to use a different name i.e. Uploads

ERROR: column “category” does not exist

I’ve recently switched from SQLite3 to PostGreSQL and came across the following error this morning:

my_app_development=> select category from codes;
ERROR:  column "category" does not exist
LINE 1: select category from codes;

The following post solved the issues in one sentence:

http://archives.postgresql.org/pgsql-novice/2007-01/msg00032.php

Yes. In postgres, unquoted column and table names are converted to lower case.

The user also added that when using PostGreSql you should either be always, or never using quotes around column names.

Creating Postcode Based Locations – Rails

My goal for today was to populate a record with all of the Australian postcodes and their corresponding suburb and state descriptions. To do this I had to complete the following:

  • 1: Download the postcode/location csv
  • 2: Create new model to contain locations
  • 3: Create a script to import a csv
  • 4: Run the script in order to populate the database
  • 5: Cleanse the data

Step #1: Download the .csv
I came across the following files after browsing a few of the Australian tech forums. I chose to use the Corra ones due to the claim that there are no license restrictions.

Once you’ve chosen your .csv, just save it in your project folder i.e. C:\my_app\

Step #2: Create a Model for your Locations
Secondly, you’ll need to create a new migration for your locations. I’ve simply called mine Code:

def change
      create_table :codes do |t|
        t.string "Pcode"
        t.string "Locality"
        t.string "State"
        t.string "Comments"
        t.string "DeliveryOffice"
        t.string "PresortIndicator"
        t.string "ParcelZone"
        t.string "BSPnumber"
        t.string "BSPname"
        t.string "Category"
        t.timestamps
     end
end

Step #3: Create a Script to Import the .csv
The third step is to create a script that allows you to import the csv. This is made simple thanks to a comment left by “Gianluca” on the following blog post:
http://erikonrails.snowedin.net/?p=212

Simply create a new file within your tasks folder and add the following code:

#my_app/lib/tasks/import.rake
 
require "csv"
desc "Import CSV file into an Active Record table"
task :csv_model_import, [:filename, :model] => :environment do |task,args|
  firstline=0
  keys = {}
 
  CSV.foreach(args[:filename]) do |row|
    if (firstline==0)
      keys = row
      firstline=1
      next
    end
 
    params = {}
 
    keys.each_with_index do |key,i|
      params[key] = row[i]
    end
 
    Module.const_get(args[:model]).create(params)
  end
end

Step #4: Run the script in order to populate the database
To run the script simply run the following command:

chris@chris-VirtualBox:~/my_app$ rake csv_model_import[codes.csv,Code]

Step #5: Cleanse the data
Cleansing the data is a little tedious, however one tip is to remove all locations that do not have a category value of “Delivery Area”.

Ahwell, that’s all I’ve got for now – let me know if you have any trouble.