Rails 3.1 on Heroku, TLDR Version
Posted by John in Ruby and Rails on December 13, 2011
Create the app
- If not installed, install PostgreSQL
rails new app_name -T -d=postgresqlcd app_name/rvm --create --rvmrc 1.9.2@app_namervm rvmrc trust- Edit .Gemfile
bundle installcreateuser -P -S -R -d app_name(no to ‘Superuser’ and ‘Create roles’, yes to ‘Create databases’)rake db:create- Create procfile in app root.
foreman startand check http://0.0.0.0:5000/
Push to Github
- Edit .gitignore
git flow initgit add .git commit -m 'Project skeleton'- Create app_name on Github
git remote add origin git@github.com:JohnPlummer/app_name.gitgit push -u origin developgit push -u origin master
Push to Heroku
heroku create app-name --stack cedargit flow release start '0.0.1'git flow release finish '0.0.1'git push --allgit push heroku masterheroku run rake db:seedheroku open
Does Email Obfuscation Work?
Posted by John in JS, HTML and CSS on December 7, 2011
It seems to be common wisdom that if you put your email address on the web in plain text you will get spammed (more). There are various ways to try and hide an email address from various crawlers whist still displaying it to visitors, most involving javascript and relying on the crawlers not having javascript or not having the script to un-obfuscate your email.
My requirements for any solution are, in order of importance:
- A clickable link.
- Gracefull degrade for users without javascript.
- Users should be able to copy the email address to the clipboard.
- It should be easy for me to insert an email address on a page.
- It should reduce spam.
A standard <A> tag with a mailto link satisfies the first four requirements but doesn’t help reduce spam. An address written as ‘name@*removethis*domain.com’ will probably defeat all the crawlers but fails the first three requirements and is painful for your visitors.
Silvan Mühlemann did some research between 2006 and 2008. He found three methods that stopped all spam but, to different extents, don’t meet the first four requirements. more interesting to me is he found using ‘name AT domain DOT com’ drastically reduced spam as did building the address with javascript.
With this in mind my current preference is to enter an email address as:
Then call a de-obfuscate function on document ready
This provides a clickable and copyable link to most visitors and degrades to ’name AT domain DOT com’ for those without javascript.
I suspect that this method will cause the least pain for most visitors and stop some of the spam but it depends on how much the crawlers have moved on since Silvan’s test. One way to find out is to rerun the test.
I’ll just use the top 5 results from Silvan’s test and add plain text for comparison purposes. I intend to let the test run but will report back when I get something statistically significant. If you know of any other tests running or run recently let me know and I’ll include links at the end of the post, you can mail me at john AT johnplummer DOT com
.
Using plain text
email: pt@johnplummer.com
Using AT DOT
email: ad AT johnplummer DOT com
Using AT DOT and javascript to de-obfuscate
email: js AT johnplummer DOT com
Using code direction
email: moc.remmulpnhoj@dc
Using style=”display:none”
email: dn@johnplummer.com
Using ROT13
email: rot mail
Installing Postgresql and pgAdmin
Posted by John in Ruby and Rails on December 6, 2011
The default database for Heroku is PostgreSQL and, while you could use SQLite for development and Postgres for production, there are some inconsistencies between the two. Ideally you would use the same version of the database server but currently Heroku uses version 9 for dedicated databases and 8.3 for shared databases and seem to recommend you install the latest version for development.
Install Postgres with Homebrew with brew install postgresql and follow the instructions after the install to initialise a database.
initdb /usr/local/var/postgres
The database server can be set to start at login with
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.1.1/org.postgresql.postgres.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
But I prefer to add aliases to .bashrc to start and stop the server:
alias pgs='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start'
alias pgq='pg_ctl -D /usr/local/var/postgres stop -s -m fast'
Postgres can be managed with the command line utility psql but, as much as I like the command line, I don’t really want to have to write SQL to edit or remove a user role. PgAdmin is a free GUI for Postgres management. Once installed, ensure the Postgres server is running then run pgAdmin and connect to the server.
In the server properties add a name for the connection and add your login as the username.

pgAdmin provides the management tools you would expect.

Setting up Rails on a Clean Install of Mac OS X Lion
Posted by John in Ruby and Rails on October 25, 2011
I realise that OSX does not have a registry to clog up but I have just reinstalled Lion on my Macbook Air; probably a hang up from Windows. This is a checklist for installing Homebrew >> Rails.
- Install XCode 4.1 (There are issues with the 4.2 compiler and certain rubies and gems)
- Install Homebrew
- https://github.com/mxcl/homebrew/wiki/installation
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
- Ensure Homebrew apps are in path before Mac apps
export PATH=/usr/local/bin:$PATHor edit /etc/paths
- Install Git
brew install git(thanks Brendan)
- Install SSH keys
- Generate or copy from ~/.ssh from a backup.
- Install RVM
- https://rvm.beginrescueend.com/rvm/install/
bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )- Add
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.to bash_profile - Restart shell
- Run
rvm | head -1
- Install latest Ruby and set as default
rvm install 1.9.3(make sure you are using Xcode 4.1)rvm --default use 1.9.3
- Install Rails
gem install rails
I keep all my .config files in Dropbox and run a bash script to create aliases in my home folder.
Changing Three Finger Swipe to Back in Lion
Lion introduces some new gestures which unfortunately breaks some gestures I am used to such as a three finger swipe to move back in Chrome. Easily fixed by opening System Preferences > Trackpad > More Gestures and changing swipe between full screen apps to use four fingers and swipe between pages to use three.
Rails default scripts and jquery-rails
Posted by John in Ruby and Rails on June 9, 2011
I recently had an issue in a (3.0.7) rails app where link_to some_path, :method => :post was calling the action associated with GET, this was caused by not having a link to jquery_ujs.js (I’m using jquery so don’t want rails.js) in my html output.
Googling turns up a number of suggestions to add jquery_ujs.js and the other jquery script files to the default javascripts by amending config/application.rb. This works ok but it’s not ideal as you probably want minified versions of the files in production and non minified in development.
Digging into the jquery-rails gem reveals railtie.rb:
This sets the default scripts before config/application.rb is run. Unfortunatley with application.rb as it is generated if you run rails new with the -J switch (you probably don’t want prototype if you plan to use jquery) these defaults are overridden. Commenting out
From config/application.rb allows the jquery-rails scripts to load but doesn’t allow you to add to the defaults. I did submit a pull request amending the railtie to run it’s code on before_initialize (after config/application.rb has been run). This allowed defaults to be set and the jquery defaults appended but as Indirect pointed out, this prevents you overriding the jquery defaults in application.rb.
The way to add your own scripts is to append them to the defaults using:
This works but there are 2 related issues; jquery_ujs.js should be the last script loaded as it needs to hook into any submission events for ajax elements and it can’t unless scripts for those events are loaded before it.
With the solution above we have application.js (added by Rails) and our scripts loaded after jquery_ujs.js. Our scripts can be placed first by changing application.rb to:
This still leaves application.js loaded after jquery_ujs.js but that should be fine as long as there aren’t any submission events scripted in it.
Using the railtie to set the default scripts then allowing you to append or override these defaults is a neat solution by the jquery-rails gem, unfortunately it doesn’t seem that many know about it as there seems to be a lot of advice to add the jquery script files directly in config/application.rb. It looks as these issues will go away in Rails 3.1 anyhow.
Rails Autoload and Ruby Require
Posted by John in Ruby and Rails on June 7, 2011
Rails methods of locating and loading files is mostly based on convention but is built on top of Ruby’s methods.
Ruby
The basic way to tell a ruby to load a file is to require it, e.g. require some_file will load some_file.rb if the file has not already been loaded. load some_file.rb will load the file (and execute any code) regardless of if the file has been loaded already.
In 1.9.2 the path that Ruby looks for these files includes:
- A number of directories in your Ruby install.
- Directories of any gems loaded
- Directories you have added to $LOAD_PATH, Rails adds a number of app directories to this variable. To see exactly what is added run puts $LOAD_PATH in both irb and a rails console.
To require a file relative to the file doing the requiring use require_relative some_directory/some_file.
Require and load both load the file when execution reaches the relevant require or load call, autoload some_file will not load the file until a module in the file is called.
Rails
In addition to adding app directories to the $LOAD_PATH, Rails loads all *.rb files in /config/initializers/ when the application starts.
Rails also has an autoload_path, If Rails comes across an uninitialized constant it will check any paths in autoload_path for a file matching the name of the constant e.g. when execution hits an unrecognized constant MyConstant it will check for a file called my_constant.rb in any paths included in autoload_paths.
The autoload_path in 3.0.7 is empty, to add the lib directory to it, add config.autoload_paths += %W(#{config.root}/lib) to config/application.rb.
If the module name does not match the file name then the file will need to be required in the normal way.
Rails also allows you to require a file with require_dependency. Require_dependency works exactly like Ruby’s require except, in development mode, it reloads the file when a view is refreshed allowing you to make changes to the required file and see them by refreshing the browser.
Incude and Extend
There seems to be some confusion between require and include/extend but they are totally unrelated. Require and similar deal with loading files from the filesystem. Include and extend deal with including code from one module in another. A class the includes a module allows an instance of the class to access code from the included module. A class that extends allows the class to access code from the module called.
Launching irb from vim
Posted by John in Ruby and Rails on May 19, 2011
You can run irb from vim with the command !irb.
Running !irb -r % should run the current file in irb then leave irb open. Unfortunately ruby 1.9.2 removed the current directory from the load path which causes this to fail. You could construct a path but it is easier to add the current directory back in, especially if you are using irbtools which does it for you.
Once irbtools are installed add:
map <F2> <ESC>:w<CR>:!irb -r %<CR>
to .vimrc.local which will write then load the current file into irb.
Edit: I was getting some weirdness with the irb prompt with irbtools so I have removed it and added
$: << ‘.’ if RUBY_VERSION >= ’1.9.2′
to my .irbrc file to add the current directory to the load path.
Recovering from ‘the application finder can’t be opened’
Every now and again I get an error when opening finder:
‘the application finder can’t be opened’
Whilst I am sure a restart would fix it, I normally don’t want to restart as I am in the middle of something. The solution is to run the following in terminal:
/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder &
(don’t forget the &) If this continues to happen I should probably try and find the cause, more likely that I will just map the command to a bash alias though
.
MacVim, NerdTree, Janus Annoyances
I’ve decided to use Vim for a few weeks at least and am enjoying it. There are plenty of posts on using Vim as a Ruby IDE and switching from your IDE of choice. Most of the TextMate to Vim posts seem to recommend Janus and for good reason.
Janus adds a number of plugins and settings to Macvim including NerdTree. NerdTree displays a project drawer in a window which is in a vertical split, normally alomgside your working buffer. The first problem is that if you close your working buffer with :bd or :bw, the NerdTree window will fill the MacVim tab, to cycle to the next or previous buffer then entails resizing splits which is not what I want.
Janus solves this with a function in .vimrc:
This function closes the MacVim tab (i.e. Cmd + W) if there is only one tab and it contains 2 windows, one of them with the NerdTree buffer. Unfortunately this doesn’t consider buffers that are not currently displayed. If I want to close MacVim or the MacVim current tab, I’ll use Cmd + Q or W, when I delete a buffer I want the window to display the next buffer.
To disable the Janus function add an override to .vimrc.local:
It’s better to make changes in .vimrc.local and .gvimrc.local as Janus can be updated to the latest version without losing your customisations.
Luckily there is a script on the vim wiki that keeps the window open when deleting a buffer. It can be amended to ignore the NerdTree buffer, if I want to close NerdTree I will use the key mapped to toggle it:
I would like to map :bd to :Kwbd but it seems it can’t be done (I’d be happy to be proved wrong here) so have mapped Ctrl + Shift + B in .vimrc.local: