rubycoloredglasses


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


  1. RSpec Controller Tests Receiving 'No route matches' Error

    I’m developing a Rails engine gem for the company I’m working for, which will provide an API for the applications we’re using. The gem I’m creating will be used with a Rails 3.0.9 system, using Rspec-Rails version 2.10.1. I had a route to my API interface setup in the config/routes.rb file like so:

    Rails.application.routes.draw do
      match '/companyname/api_name' => 'CompanyName/ApiName/ControllerName#apimethod'
    end
    

    When I added a ‘get’ request call to my controller test, I was getting this error:

    Failure/Error: get :apimethod
    ActionController::RoutingError:
      No route matches {:controller=>"company_name/api_name/controller_name", :action=>"apimethod"}
    

  2. Cubase Installation Failure

    I recently ran into issues installing Cubase 4 on my Mac running Snow Leopard. I uninstalled Cubase 5 Essential, thinking that this was causing a conflict, and thus stopping me from installing an older version. This wasn’t the case I tried to install Cubase 5 Essential again, and I got the same type of error with it’s installer. I received Cubase 6 in the mail today and tried to install it…only to receive the same type of error:


  3. Generators Not Working in Rails 2.3.8

    I’m currently working on a gem that is going to use a generator to create files in a Rails 2.3.8 application. One of the applications we’re still using is using Rails 2.3.8, so I have to make a gem compatible with that version of Rails.

    I installed Bundler v1.0.22 and configured it to work with the Rails app, and then followed many instructions and various configurations to get my generator classes to load. Every time I would try to run the generator however it simply gave me the error “Couldn’t find ‘hello’ generator”.


  4. Establishing New Ruby Environment in a Folder using RVM

    I know this is documented on the official RVM website, but I hate having to look it up over and over again each time I want to create a new RVMRC file.

    $ mkdir -p ~/projects/rails2test
    $ cd ~/projects/rails2test
    $ rvm --rvmrc --create 1.8.7@rails2test
    $ cd ..
    $ cd rails2test
    ==============================================================================
    = NOTICE                                                                     =
    ==============================================================================
    = RVM has encountered a new or modified .rvmrc file in the current directory =
    = This is a shell script and therefore may contain any shell commands.       =
    =                                                                            =
    = Examine the contents of this file carefully to be sure the contents are    =
    = safe before trusting it! ( Choose v[iew] below to view the contents )      =
    ==============================================================================
    Do you wish to trust this .rvmrc file? (/Users/jmiller/Documents/rails2-apps/.rvmrc)
    y[es], n[o], v[iew], c[ancel]> y
    
    

  5. History of the Canonical Gem Host for Ruby Gems

    The default repository for downloading gems using RubyGems was originally RubyForge.org. This was likely because the RubyGems project was hosted only from RubyForge. This meant that when you ran ‘gem install rails’, your RubyGems installation was configured to download the gem from ‘gems.rubyforge.org’. In August of 2008 Github started gaining popularity amongst the Ruby community after it started providing it’s own gem server via gems.github.com. This resulted in many Ruby developers configuring RubyGems to use the Github server as a secondary source of gems.

    In August of 2009 a new gem hosting repository came onto the scene known as GemCutter, aiming to resolve issues caused by how Github and RubyForge were handling hosting of gems. In September they decided to move the service to RubyGems.org. In October of Github discontinued building and serving gems from gems.github.com, and RubyGems.org became the official default host for Ruby gems.


  6. Using Serialize Option with ActiveRecord Objects

    Documentation seems to be more available on how to build forms with check boxes or a multiple select field for ActiveRecord objects that have a has_many or has_many_and_belongs_to association with other ActiveRecord objects. This article shows you how provide a multiple select form based on a custom defined array, with the selected options stored in a single attribute of your ActiveRecord object.

    Lets say you are working on a form for a blog post that needs a multi-select field of statically defined adjectives, with the one or many adjectives saved to one field for the post.


  7. Save the Tests, Don't Throw Them Away

    So it’s been several weeks since I started using test driven development. I’m using FactoryGirl instead of fixtures, because I’ve heard that fixtures are limiting. I’d rather just write Factories from the beginning. I’m also using standard Test::Unit based unit and functional tests. Haven’t touched on integration testing yet.

    As I’ve gone along and written these tests, I’ve learned how to do things effectively and am carving out my own style.

    For instance you could either test for a link with the content ‘Edit’ on the page, or you could add an ID or class to the link and test for the presence of that link with that class. I found that I’d rather do both just to ensure that the button is or is not present in a certain circumstance.


  8. Factory Girl Associations and Records Persisting Across Tests

    I just recently started to adopt test driven development practices. The project I’m working on needs to get done soon, and I didn’t want to get held up learning Rspec. After much consulting with other developers at the company I work for, I had decided to use basic Test::Unit tests with FactoryGirl factories instead of fixtures, and adopt Shoulda if a scenario arises where the options it provides (contexts) are needed.

    So far things have been running well, and I’m starting to understand just how important testing is. You don’t have to write tests for every single thing you do, but if you implement some sort of feature that you seriously don’t want to break at some point in the future, setup a test for it. Once you setup a test for one type of feature, you can re-use the code later for similar testing. So don’t worry about how long it takes the first time around, it will pay off later when that function isn’t broken because you caught it. I didn’t realize it, but errors you didn’t expect it to directly catch, like the dreaded “undefined method ‘foo’ for nil:NilClass” exception, also popup periodically and alert you that you broke something, even though your test wasn’t built to catch those. This is nice because you might change something in a model, and then all of a sudden something in a view is broken.


  9. Generating Test File Stubs for Existing Models, Views, and Controllers

    I’ve noticed that if you install certain testing gems, like Factory Girl, or Rspec, that your Rails application will create test files for these libraries instead of using the defaults. Even further you can configure the generators used by your Rails app in /config/application.rb

    # Configure generators values. Many other options are available,
    # be sure to check the documentation.
    # http://edgeguides.rubyonrails.org/generators.html#customizing-your-workflow
    
    config.generators do |g|
      g.stylesheets false
      g.test_framework :rspec
      g.fallbacks[:rspec] = :test_unit
      g.fixture_replacement :factory_girl
    end
    

  10. Rails 3 and Subclasses Method

    I was just trying to create coding that reflectively loads the subclasses of a class I’ve defined. The idea is that as new subclasses are added, the script I’m writing can detect which ones are present and inform a remote API that support for a specific API feature is available.

    I had executed the method once on the class, and it did return the name of the subclass that I had defined and checked for the existence of in the Rails console environment. Then I added other subclasses, reloaded (reload!), and ran the method once again. This time I got nothing but an empty array returned. It turns out that Rails 3 uses autoloading for classes…so the subclasses have to be referenced at some point, and thus loaded into memory, before the subclasses method will include them in the list.