rubycoloredglasses


I'm Jason, a web applications developer in the Denver, Colorado area.


  1. Redirect_to not working

    I was just working on a Ruby on Rails controller method that receives information from the previous form via HTTP POST. I coded it so that if certain form variables weren’t present it would set a flash message and redirect to the form page. I tried and tried and still the redirect wasn’t working. I reset my web server, and even restarted my computer, but stil this didn’t resolve the issue.

    I then realized that perhaps redirects aren’t possible with HTTP POST’s, only GET requests. I ended up just creating a generic view for displaying errors, and will render that view and then ‘return FALSE’ inside of the if statement when an error is detected.


  2. Advanced Use of Will_Paginate

    I’m building an index of contacts, displayed with paginated links provided by will_paginate.

    The wiki for this plugin advises you on how to do setup your controller method, and what to put in the view to obtain a simple set of links, such as:

    # /app/controllers/contact_controller.rb
    def index
      @contacts = Contact.paginate :page => params[:page], :per_page => 10, :order => 'created_at DESC'
    end
    

  3. Getting File object for Paperclip Attachment via S3

    I’m working on a project where we are using the Paperclip plugin for Ruby on Rails for file handling and associations with other models.

    I’m working on a CSV import option right now, using this tutorial to help me get a head start on how to break the contents of the file up into rows and columns. I’m not passing the file directly from a form to the controller method, but I’m opening the file that has already been saved after being uploaded via AJAX.


  4. Issues with MacPorts After Upgrading to OS X Lion

    I realized this morning that I was having dependency issues with ImageMagick on my Mac, which I installed using MacPorts. I had recently upgraded to Mac OS X Lion, so it made sense that I needed to update the software to resolve the issues, much like I had when I upgraded to Snow Leopard.

    I found this article that provided steps for migrating MacPorts for Lion, but I kept getting this error when I tried to uninstall all the packages:

    warning: Failed to execute portfile from registry for apache2
    @2.2.17_1+preforkmpm too many nested evaluations (infinite loop?)
    Warning: Failed to execute portfile from registry for apache2
    @2.2.17_1+preforkmpm too many nested evaluations (infinite loop?)
    Warning: Failed to execute portfile from registry for apache2
    @2.2.17_1+preforkmpm too many nested evaluations (infinite loop?)
    Warning: Failed to execute portfile from registry for apache2
    @2.2.17_1+preforkmpm too many nested evaluations (infinite loop?)
    

  5. Error: 'unintitialized constant MySQL' with Rails 3 on Snow Leopard Mac

    I just installed Rails 3 on my iMac, which is running Snow Leopard. I’m trying to build a web hosting website/billing system/management system. I configured the app to use MySQL in /config/database.yml like so:

    development:
      adapter: mysql
      encoding: utf8
      database: hosting_development
      username: root
      password:
      host: 127.0.0.1
    

    I had to do this because I created the Rails app without specifying that I didn’t want sqlite3. I then ran rake db:create and I got this error:


  6. Installing PHPdoc for Ubuntu for use with Command Line

    I wanted to install PhpDocumentor for use on my server so that I could generate documentation from the command line. I found this article, which instructed me to somehow change the PEAR setting for data_dir. I installed PhpDocumentor in my web root and it just didn’t work and gave me a bunch of errors in the browser.

    I uninstalled the package via PEAR (ala ‘pear uninstall phpdocumentor’ command), then deleted the folder. I just wanted to install it in the proper path so that it’s available from the command line using ‘phpdoc’.

    I downloaded the source of PhpDocumentor directly from SourceForge, and it had phpdoc available in the folder, but I’m not sure which folder to put it in so that it’s in my path for the command line.


  7. Ruby on Rails session - Access from PHP

    If you need to access a Ruby on Rails session from a PHP application running under the same domain, you can do this by splitting the string in the cookie by the ‘–’. Thanks to Frederick Cheung for pointing this out.

    Here is coding I added to my PHP script which was running from a path under the same domain. Unfortunately the data returned is in Marshal format, and there isn’t a Marshal.load function for PHP to get the values easily.

    $raw_session_string = $_COOKIE['_app_session'];
    $data_split = explode ('--' , $raw_session_string);
    $encoded_session = $data_split[0];
    $marshal_data = base64_decode($encoded_session);
    echo "<pre>marshal_data:". print_r($marshal_data,1) ."</pre>\n";
    

  8. Obtaining Request Domain Name for Ruby on Rails

    I’m using Rails 2.3.8. To obtain the domain name for the website being requested (i.e. mysite.com, mysite.net), just reference ‘request.host’.

    ruby@host = request.host
    

    You can only reference request.host in the views, or the controller.


  9. Changing Column Order via ActiveRecord Migration

    Is it possible to change the order of the columns in your MySQL (or other database) table using a migration? Lets see.

    If you check the ActiveRecord::Migration documentation you’ll see there is a method called ‘change_column’ which accepts various options.

    <tt>change_column(table_name, column_name, type, options)</tt>:
    Changes the column to a different type using the same parameters as add_column
    

    As of Rails 2.3.6 this is now available by using the :after option. You’ll have to include the field type, even though you are not modifying the type.

    Example:

    change_column :orders, :tax_rate, :float, :after => :tax_state
    

  10. Rails Performance Statistics

    Again, as I search for things, I stumble onto new tools. I just found out about a tool for monitoring the performance of Java and Ruby applications called New Relic.

    They provide a free service level for Startups and Students even.