rubycoloredglasses


I'm Jason, a web application developer in East Tennessee.


  1. Troubleshooting ActiveResource Requests

    I’m currently working on an app which integrates with the HighRise API using the Highrise Ruby wrapper gem. The classes defined by the gem rely on ActiveResource, the Ruby library included with Rails for interfacing with RESTful resources like the HighRise API.

    Sometimes I’m not sure if the requests being made via the commands I’m using are making the right calls on the HighRise API.

    I found this thread on StackOverflow: How do I view the HTTP response to an ActiveResource request?. The solution I found helpful was this one on overriding ActiveResource::Connection so that it outputs the debug output. This worked for me even with Rails 3.1.


  2. Example Rake Task

    Here is example rake task code which you can use and modify the next time you’re setting up new Rake tasks for a Rails app from scratch. The example includes the syntax for setting the default task, running multiple tasks in order, and a task which includes multiple arguments. This coding syntax works with Rails 3.1.

    # /lib/tasks/mytask.rake
    namespace :mytask do
      task :default => 'mytask:full_run'
    
      desc "run all tasks in proper order"
      task :full_run => [:example_task_one, :example_task_two] do
        puts "All tasks ran and completed at #{Time.now}"
      end
    
      desc "example task without arguments"
      task :example_task_one => :environment do
        # task code goes here
        puts "Example Task One completed"
      end
    
      desc "example task with arguments"
    
      task :example_task_two, [:arg1, :arg2] => :environment do |t, args|
        # if no arguments, display docs
        if args.count == 0
          puts ''
          puts "rake mytask:example_task[arg1,arg2]"
          puts "arg1: description of first argument"
          puts "arg2: description of second argument"
          puts ''
          puts "example:"
          puts "rake mytask:example_task[123,'456']"
          puts ''
        else
          # task code goes here
          puts "Running task with arg1: #{args.arg1}, and arg2: #{args.arg2}"
          puts "Example Task Two completed"
        end
    
      end
    end
    

    Special thanks to Jason Seifer for his Rake Tutorial which made this possible.


  3. Adding a New User in Ubuntu

    Adding User

    When setting up a new website manually on an Ubuntu server you need to establish a user account with a home directory, and Bash shell access to the server.

    useradd -m testuser -s /bin/bash
    

    After creating the account you’ll want to assign a password for the account.

    passwd testuser
    

  4. Resolving issues with Namespaced Models in Rails 3.1.0

    I recently was tasked with upgrading an application from Rails 2.3.8 to Rails

    1. I choose to upgrade it to Rails 3.1, because why upgrade once and then have to do it again later.

    After upgrading and testing many points of the system locally, I was ready to push the upgraded application to the production server. After pushing it out I started to notice that a certain rake task was failing to run via a cronjob I had setup. This rake task worked with certain non-ActiveRecord models, which I had setup with a hierarchy which utilized inheritance and namespacing.

    Under Rails 2.3.8 I had a file, /app/models/rets_map/rets_map_base.rb. The error I was receiving from the rake task involved some sort of issue loading the class from this file.


  5. Paperclip error with non-image file

    Recently I updated to Rails 3.1 from 2.3.8 for a project I’m working on. Paperclip version 2.4.5 was working perfectly well for me locally on my Mac OS X 10.7.2 laptop, with ImageMagick version 6.7.3-1.

    We had just launched the upgraded Rails 3.1 application to our production server, which went smoothly, but upon my checklist of pages to test (we don’t have test suite in use yet) I noticed that the file upload was failing. Further investigation showed that the object using paperclip was failing to save:

    upload.errors.inspect:
    
    #["/tmp/stream20111205-28441-10oj2ck-0.csv is not recognized by the 'identify' command.", "/tmp/stream20111205-28441-10oj2ck-0.csv is not recognized by the 'identify' command."]}>, @base=#>
    

  6. Issues with RVM

    I recently decided to check out BrowserCMS, to evaluate how it work and decide to use it…or RefineryCMS. I didn’t expect that BrowserCMS would require Ruby 1.9.2. I’ve been running with Ruby 1.8.6 or 1.8.7 for quite a while now without any issues. It looks like it was time that I install RVM: Ruby Version Manager.

    I read through the documentation, followed the instructions to ensure that prerequisite software was installed. I specifically made sure that certain commands that it stated would be needed were all in my command line path under /opt/local, where the MacPorts all install. I try to maintain a command line environment that’s almost entirely dependent on MacPorts. Prior to ensuring this I ran into issues where software I’ve tried to install was using one utility or library provided natively by Mac OS X, while using some other utility or library I’ve installed separately, conflict due to differences and stop certain software from building properly upon install.


  7. Setting up Ubuntu for Rails App via Passenger

    These instructions apply to Ubuntu 11.04 (natty).

    Run the following to install Ruby, the Apache2 web server, Curl Development Headers with SSL Support.

    sudo apt-get update
    sudo apt-get install build-essential
    sudo apt-get install ruby ruby1.8-dev
    sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev libcurl4-openssl-dev
    

  8. Formtastic use of semantic_form_remote_for

    With the update to Formtastic version 2.0.0.rc1 the ‘semantic_remote_form_for’ method was removed and support was added to ‘remote_form_for’ used like this:

    <%= semantic_form_for @contact, :remote => true, :url => '/contact' do |f| %>
    

    This is to conform with the new Rails 3 update where form_remote_for is replaced with form_for with a remote hash value passed to it as an argument.


  9. Exporting Routes in Rails 3

    I’m currently upgrading a project I’m working on from Rails 2.3.8 to Rails 3.1. As part of this upgrade I need to test the entire application for issues, because we haven’t actually written any tests.

    To help with this, I’d like to export all the routes so that I can test them one by one, and keep track of what I’ve tested and fixed already.

    Typically I output the routes by using this command from the root of the Rails app:

    rake routes
    

  10. Using URL Helpers in Models or Rake Tasks

    If for some reason you need to use URL helpers which are based on the routes you’ve defined in Rails 3.1, simply add the following to the model method or rake task:

    include Appname::Application.routes.url_helpers
    

    Make sure you replace ‘Appname’ with the name of your app, which should be the same name as the root folder for your application. You can also obtain it from /config/application.rb where it is defined like so: