Internet Explorer 7 gets Authenticity Token Invalid from Rails

20 04 2009

Recently we had an issue where a client was unable to access his CMS. He was receiving an error ‘422 The change you wanted was rejected’. This was the production pretty message, caused by an InvalidAuthenticityToken error. After much debugging, trying to turn off authenticity token filtering and removing before filters I took a stab in the dark.

The problem was that the site was on a sub-domain with an underscore in the name abcd_efgh.demosite.com. While all other browsers behaved perfectly, Internet Explorer 7 (and I believe 6 as well) had a tantrum and wouldn’t accept cookies. The solution was to change the site’s underscore to a hypen, making it abcd-efgh.demosite.com. Hope someone out there saves time time and hair with this one.





Vim + Screen + iTerm with 256 colours

7 04 2009

I’ve recently started using screen and have been thoroughly liking it. One issue I had was getting my full colourscheme with vim to work properly. In the end I did get it working and here I’ll outline the combination of apps and configuration that did the trick.

  1. Install iTerm Build 0.9.6.1201 from the iterm page on sourceforge. At the time of writing Build 0.9.6.20090328 was available but it cut the bottom off my letters.
  2. Open up ‘Manage Profiles’ in the bookmarks menu (or alt-cmd-b), expand the ‘Terminal Profiles’ menu and select ‘Default’. Now set ‘type’ to xterm-256color. It should be in the drop down.
  3. Download screen from Philip Halstrom’s site and extract the files.
  4. Open terminal cd into the directory with the screen files and run:
      $ ./configure --enable-locale \
         --enable-telnet \
         --enable-colors256 \
         --enable-rxvt_osc \
         --prefix=/usr/local
      $ make
      $ make install
  5. Now edit your screen config file ‘~/.screenrc’ and add the following lines:
      startup_message off
    
      #terminfo and termcap for nice 256 color terminal
      # allow bold colors - necessary for some reason
      attrcolor b ".I"
      # erase background with current bg color
      defbce "on"
  6. Now add the following lines to your vim config ‘~/.vimrc’, in this example I’ve included my colourscheme; railscasts. Make sure the first line in this example comes before any color configuration in your vimrc file.
      set t_Co=256
      colorscheme railscasts

A good deal of the information in this post came from Philip Halstrom’s post. He includes a few more lines in his screenrc, these caused jumbled letters to appear in my terminal. I’m guessing they addressed an issue with an older version of iterm that has since been resolved.





Updating Vim on OS X Leopard

7 09 2008

I recently tried to get vim up to version 7.2 on leopard (so that i could get vimball working and install plugins). In the end I settled for using the copy of vim that comes with MacVim and setting up an alias:

alias vim='/Users/logaan/Downloads/MacVim-7_2-stable-1_2\ 2/MacVim.app/Contents/MacOS/Vim'

But before I went to this extreme I tried:

  • Port: The files downloaded didn’t match the checksum.
  • Fink: The version was old but installed properly. Then didn’t work.
  • Manual Compile: Didn’t setup the right paths for plugins, etc.

If you want to do it my way grab the latest version of MacVim from http://code.google.com/p/macvim/ install, then right click on MacVim.app and show contents. Navigate into the MacOS folder and you should see ‘Vim’. Open up ~/.bash_profile in your favorite text editor and type alias vim='' then drag the ‘Vim’ application between the quotes. It should insert the full path to the app.

It’s not pretty, but it works.





Eager to please annonymous objects

6 09 2008

I’ve been doing a lot of unit testing recently, and often find myself wishing that my mock objects actually had a state. So I could push a dumb object into the system and have a look at it when it comes out, without needing to tightly couple the stubbed methods on it to the implimentation. So I’ve written up a little method that’ll help you do exactly that. It allows for javascript style annonymous objects. As soon as you try to write to an attribute or read from one the getters and setters for that attribute are created.

def annon
  # Create a new blank, basic object
  annonymous_object = Object.new

  # If a attribute is called on this object that doesn't exist
  # we want to fake it
  def annonymous_object.method_missing name, *args
    # Figure out the base name of the attribute
    method_name = name.to_s[/[^=]*/]

    # Define the getter and setter
    self.instance_eval <<-METHODS
      def #{method_name}= value
        @#{method_name} = value
      end
      def #{method_name}
        @#{method_name}
      end
    METHODS

    # Call the attribute again
    send name, *args
  end

  # Now that we've created our annonymous object
  # we'll pass it to the block. Here attributes and
  # default values to be added to the object.
  yield annonymous_object if block_given?

  # Finally return the object we've built
  annonymous_object
end




Dynamically Loaded Classes

20 02 2008

Defining the class name of rails models when we’ve already named the file didn’t seem very DRY to me. So I wanted to find out if I could load files into a class named after the file (ie. Products.rb gets loaded in to Product class)

#!/usr/bin/env ruby

# Loop over all ruby files in the classes directory
Dir["classes/*.rb"].map do |file_name|
  # Save file name without extention or directories
  class_name = file_name[/[^\/]*(?=\.)/].to_sym

  # Execute the contents of the file inside a blank object
  class_object = Class.new
  class_object.instance_eval(File::read(file_name))

  # Bind the class to the name at the highest level
  Kernel.const_set(class_name, class_object)
end

This assumes that you have a bunch of ruby files inside a sub directory called ‘classes’ and all your ruby files have ‘.rb’ extension.





Quicksilver rails project opener (revisited)

16 01 2008

I’ve switched my terminal over from Terminal to iTerm and my subversion client from SVNX to ZigVersion. So I re-wrote my opener for these new apps. Installation and such should be the same as the original.

#!/usr/bin/env ruby

#####################################################################
# This script will load all the common applications used for
# developing rails. It may be triggered by quicksilver or console.
# The full path of the project to open should be the first argument.
#
# FixMe: If iTerm is not open an unwanted window is created
#
# Applications that will be loaded:
#   Textmate at project
#   Projects mongrel server
#   Projects rails console
#   Blank console in project dir
#   Firefox at project
#   ZigVersion
#####################################################################

# Load files required for appscrpt
require "rubygems"
require "appscript"
include Appscript

# Define arrays and grab project path
tabs    = []
threads = []
project = ARGV[0]
# project = "~/Programming/rails/code_library"

# Do iterm stuff in one thread
threads << Thread.new do
  # Make a new iTerm window
  window = Appscript::app('iTerm').end.make(:new=>:terminal)

  # Create three new tabs and store in sessions array
  3.times do
    window.launch_(:session => 'default')
    tabs << window.sessions.last.get
  end

  # Change each tab to project path
  tabs.each { |tab| tab.write(:text => "cd #{project}") }

  # Start textmate, server and console from tabs
  tabs[0].write(:text => "mate ."         )
  tabs[1].write(:text => "script/server"  )
  tabs[2].write(:text => "script/console" )
end

# Open ZigVersion and Firefox, each in their own thread
threads << Thread.new { Appscript::app("Firefox").OpenURL("http://localhost:3000") }
threads << Thread.new { Appscript::app('ZigVersion').activate                      }

# Join threads together to stop premature termination
threads.each { |thread| thread.join }




PHP style boolean expressions with Ruby

8 01 2008

I’ve come to ruby and rails from php. By and large I’m loving the switch but a few things I miss. One of the big ones is that ruby evaluates an empty string and the number 0 to be true in boolean expressions.

So I wrote a quick way around it.

class String

  def to_b

    self.length > 0

  end

endclass Integer

  def to_b

    self != 0

  end

end

Now if you want to check if a string or integer would be considered false by PHP just call to_b on it. Example:

"Desu".to_b #true

"".to_b #false

10.to_b #true

0.to_b #false

Simple but it does the job .

Update: You can just use empty? for strings





Quicksilver Action : Open Rails Project

6 01 2008

I’ve been doing tonnes of rails coding lately and absolutely loving it! I noticed that my work flow wasn’t all that DRY though; Every time I start working on an app I open up the same stuff:

  • The app as a project in Textmate (mate .)
  • The app server (script/server)
  • The app console (script/console)
  • A blank terminal (sc… wait.. no)
  • Firefox (http://localhost:3000/)

So I wrote a quicksilver action that does it all for me!

#!/usr/bin/env ruby

require "rubygems"
require "appscript"
include Appscript

# Grab the project location from the command line / quicksilver
project = ARGV[0]

# Initialize variables
term = app("Terminal")
ff   = app("Firefox")
threads = []

# Activate terminal and firefox
# This will open a new window if the app was not already
# open, which can result in one too many windows
term1 = term.activate

# So we'll close the first window if there is one
term.windows[0].close unless term.windows.get.to_s == ""

# Open up a terminal window at the project.
# Then open the project in textmate from
# the terminal, all in it's own thread
threads << Thread.new do
  term3 = term.do_script("cd " + project)
  term.do_script("mate .", :in => term3)
end

threads << Thread.new do
  term1 = term.do_script("cd " + project)
  term.do_script("script/server", :in => term1)
end

threads << Thread.new do
  term2 = term.do_script("cd " + project)
  term.do_script("script/console", :in => term2)
end

threads << Thread.new do
  ff.OpenURL("http://localhost:3000")
end

# Join all of the threads to the main one
# This stops the script exiting before all
# threads have finished
threads.each do |thread|
  thread.join
end

To Install:

  • Install the appscript gem with “gem install rb-appscript”
  • Copy script to “~/Library/Application Support/Quicksilver/Actions/Open Rails Project.rb” (you’ll need to create the file)
  • Restart quicksilver

To Use:

  • Use Quicksilver to navigate to your project folder
  • Select “Open Rails Project” as the action.

Enjoy!