Rails Style Params in Python and Flask
Ruby on Rails does a great job of parsing arrays in form data and populating a params hash, for quick and easy access in your controllers. For instance Gives you params[:item] # => ["item 1", "item 2"] Flask doesn't do this work f...
Written by Sean Behan on 11/04/2018
Active Admin Rails 5 undefined method per_page_kaminari
If you run into this error in development, using Rails 5 and Active Admin NoMethodError - undefined method `per_page_kaminari' Try the following. First, make sure you have an initializer in your application. # in config/initializers/kamina...
Written by Sean Behan on 04/27/2017
Turn Off SSL Verification In Ruby
The following code will disable SSL verification when using Open SSL. OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE It is probably not a good idea to do this in production. But if you are just testing on your development machine it might...
Written by Sean Behan on 03/02/2017
Change the Default Controller Name in Rails Functional Tests
Rails will infer the controller you want to test from the class name of the test case. For example... class PeopleControllerTest < ActionController::TestCase end will test the app/controllers/people_controller.rb. To change this is trivial. Us...
Written by Sean Behan on 10/23/2013
A Couple of Usefule Snippets When Working with Textmate
Here is the snippet f path/to/directory 'search string' | open_in_mate This snippet (technically 2 snippets) will recursively scan the contents of files in a directory and then open all the files it finds in Textmate. It's useful on large projec...
Written by Sean Behan on 10/18/2013
How To Enable IFrame Support on Heroku with Ruby on Rails and Sinatra
This actually has more to do with Rails and/or Sinatra than it does with Heroku. You have to enable IFrame support in your headers explicitly. With Rails you can add this to your config/application.rb file config.action_dispatch.default_headers = ...
Written by Sean Behan on 08/25/2013
How to Add Additional Sub Directories to the Default Rails Test:Unit File Structure
# Edit Rakefile in project root # # Add a new rake test task... E.g., rake test:lib, below everything else in that file... # Alternatively, add a task in lib/tasks/ directory and plop in the same code namespace :test do desc "Test lib source" ...
Written by Sean Behan on 06/17/2012
Transform Matching Text with Gsub in Ruby and Regular Expression
Here is a gist that demonstrates how easy it is to transform text using #gsub and a block with Ruby. "Scs Epc Score".gsub (/scs|epc/i) { |i| i.upcase } # => SCS EPC Score For more helpful string extensions in Ruby check out our Ruby Gem on GitHub...
Written by Sean Behan on 06/17/2012
Rails 3 Config Auto Load Paths in Application.rb
In Rails 3 files in lib/ are no longer loaded by default. It's a snap to auto load these classes by adding the following line to config/application.rb config.autoload_paths += %W(#{config.root}/lib) This is commented out around line 16. Either rep...
Written by Sean Behan on 06/17/2012
How To Get A List of All Registered Mime Types in Rails
When mime types are registered they are placed in a hash constant EXTENSION_LOOKUP in the module Mime. For reference, the file with the relevant code is in rails/action_pack/lib/action_dispatch/http/mime_type.rb available on Github at https://github.com/r...
Written by Sean Behan on 06/17/2012
Rails Send_File in Production Delivers an Empty File
If you're running Rails in production it will by default be configured to let apache or nginx send files for you. If you're handling file downloads yourself with send_file send_file("path/to/file.txt") you will notice that the downloaded files are empty...
Written by Sean Behan on 06/17/2012
Simple String Concatenation of a Collection Written as a Helper for Rails
At Railsconf last week I took Greg Pollack's online course Rails Best Practices. The interface is gorgeous and the instructions are excellent. One of the lessons involved taking a partial and moving it into a helper. I was reminded how difficult such a si...
Written by Sean Behan on 06/17/2012
Installing Ruby on Rails 3, MySQL, Git, Ruby Enterprise Edition, Passenger (Mod_Rails) on Ubuntu with Rackspace Cloud.
Short and sweet. Here all the commands I run in this order to set up a brand new box. It usually takes about 10 - 15 minutes on a 256 MB RAM instance. Compiling Ruby Enterprise Edition, which is super easy, will take the most amount of time. It will seem ...
Written by Sean Behan on 06/17/2012
Rails 3 disable_with Does Not Work with Ajax Remote Form_for
It appears that the :disable_with option on the submit_tag form helper method does not behave as expected with remote forms. I'm not sure if this is a bug or not. But the fix is pretty straight forward, but perhaps a little difficult to trouble shoot. Usi...
Written by Sean Behan on 06/17/2012
Rails Select Tag and Onchange Event Calling a Remote Function with Default Option Selected
Here is a little code snippet that will fire off a request to update_client_path when you change the select field. This stands alone, rather than being apart of a larger form. The Client::CATEGORY argument is a hash, which I populated manually in my Clien...
Written by Sean Behan on 06/17/2012
Ruby Enterprise Edition and Passenger ./script/console production fails and instead returns Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org
After installing Ruby Enterprise Edition, REE, and Passenger on Ubuntu you may see this error message when you run script/console for the first time ./script/console production # => Loading production environment (Rails 2.3.5) Rails requires RubyGems >=...
Written by Sean Behan on 06/17/2012
Renaming Routes in Rails 3 with :As and :Controller
Renaming routes in Rails 2.* is straight forward. It goes something like this. ## config/routes.rb map.resources :users, :as => :members This will give you users_path and form_for(User.new) helpers, for instance, mapping the url to /members instead of ...
Written by Sean Behan on 06/17/2012
Load All ActiveRecord::Base Model Classes in Rails Application
Here is a simple rake task which will instantiate all of your Active Record models, provided that they are located in the RAILS_ROOT/app/models directory. Interestingly, all plugin models are instantiated by default when you run the task, for instance, if...
Written by Sean Behan on 06/17/2012
How Beautiful is Ruby?
Working with Ruby and in particular Rails, it's easy to take the beauty inherent in the language for granted. I mean look at this code. If you read it aloud to yourself, it reads like an english sentence that any non programmer can understand. Forum.cate...
Written by Sean Behan on 06/17/2012
Deploying to Dreamhost
Remember to include the host declaration in the database.yml file when you deploy to Dreamhost. Dreamhost does not use "localhost" which is typically the default setting when using the mysql adapter and developing locally or even on a small site. At lea...
Written by Sean Behan on 06/17/2012
Workshop Dog
Workshop Dog is a free events calendar for dog training workshops and group lessons. Users can list their training workshops as well as create a business profile. Jobs may be posted to the site for a small fee. Events that are created are distributed to...
Written by Sean Behan on 06/17/2012
Natural Dog Training Buzz
NDT Buzz is a companion news site to Natural Dog Training. News and information about Natural Dog Training is generated on the web from many different sources. Blogs, tweets and comments are aggregated using the Feedzirra Ruby Gem. The site lets NDT use...
Written by Sean Behan on 06/17/2012
Active Record Find Methods
Active Record find methods for selecting range from http://charlesmaxwood.com/notes-from-reading-activerecordbase/ Student.find(:all, :conditions => { :grade => 9..12 }) return a range Student.find(:all, :conditions => { :grade => [9,11,12] }) will retu...
Written by Sean Behan on 06/17/2012
Hacking Rails Plugins
Using the Acts as Taggable On plugin to add categories to a model class, I wanted to override the to_param method and place the name attribute in the url. The plugin, installed as a gem, source shouldn't need to be hacked in order to accomplish this. The ...
Written by Sean Behan on 06/17/2012
Setup Wildcard Subdomain on Localhost for Development Work without /Etc/hosts TomFoolery
Step 1. Open up your browser and visit http://www.hexxie.com. You can also go to anything.hexxie.com and everything.hexxie.com, which will resolve to your local machine (assuming it's localhost at 127.0.0.1). How it works Super simple. I just pointed hex...
Written by Sean Behan on 06/17/2012
Extending Rails Form Builders
Extending forms in Rails is simple and will greatly reduce the amount of code in your views. This example is taken right from the Agile Web Development book on Rails(2.1.*) with one minor tweak. I want to pass a label argument along with the field name so...
Written by Sean Behan on 06/17/2012
Ruby Strftime Day Without the Leading Zero
%e # rather than %d Time.now.strftime("%e")
Written by Sean Behan on 06/17/2012
Install and Serve a Rails Application from PHP Subdirectory Using Apache, Phussion Passenger and Ruby Enterprise Edition
Here is how to install a Rails application out of a subdirectory (rather than as a subdomain) with the Apache web server(Apache2). In this example I'm going to use my own blog which is a Wordpress installation and serve a Rails application from the subdir...
Written by Sean Behan on 06/17/2012
Placing an Authenticity Token in a Rails Form
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
Written by Sean Behan on 06/17/2012
Ruby on Rails, jQuery and YUI API Docs Available as Mac OS X Dictionary Binaries
I came across an awesome tool this morning. Priit Haamer has chunked Ruby on Rails, jQuery, and some of YUI documentation into native Mac OS X dictionary binaries. This lets you search those API docs from Spotlight, TextMate, any application that uses the...
Written by Sean Behan on 06/17/2012
A Through Z
How to print the alphabet in Rails very easily. ("A".."Z").each {|letter| link_to letter, "/#{letter"} "A".upto("Z") {|letter| link_to letter, "/#letter"}
Written by Sean Behan on 06/17/2012
Trouble Using Attr_Accessor in Rails Models and Forms
You might use the attr_accessible method to create getters and setters for a class that has attributes which don't map directly to corresponding fields in a database. For example let's take the scenario where you are processing a credit card transaction. ...
Written by Sean Behan on 06/17/2012
David Heinemeier Hansson - Ruby on Rails, Startups, Culture
[youtube]http://www.youtube.com/watch?v=sb2xzeWf-PM[/youtube]
Written by Sean Behan on 06/17/2012
Destroy Children In Rails 2.3
In the parent class place the following code has_many :posts, :dependent => :destroy This used to be accomplished with has_many :posts, :dependent=>true Check out the Rails docs for more information http://api.rubyonrails.org/classes/ActiveRecord/As...
Written by Sean Behan on 06/17/2012
Make Rails Lib Module Methods Available to Views
If you create a module in the lib/ directory of your Rails application you won't have access to those methods in your views. If you don't want to put those methods in a helper file, you need to add a method to your module that makes them available for you...
Written by Sean Behan on 06/17/2012
Installing Feedzirra RSS Parser on Ubuntu 8
I recently watched a RailsCasts episode (http://railscasts.com/episodes/168-feed-parsing) on parsing and persisting RSS feeds using Feedzirra. It took a little setting up on Ubuntu because it relies on some libraries that aren't included with Ubuntu > 8. ...
Written by Sean Behan on 06/17/2012
Fixing MySQL for Rails 2.2 Development on Mac OS X
Oh what trouble Rails 2.2 and MySQL (on Mac OS X) can be. Rails, as of version >= 2.2, no longer comes bundled with the MySQL adapter. This means you'll need to install it yourself, but it appears that the gem for installing it is also broken. This will ...
Written by Sean Behan on 06/17/2012
Rails: Expiring a cached page with namespaces and sweepers
I've got some pages that are cached using their permalinks on the filesystem, such as http://example.com/about-us.html which will need to map to RAILS_ROOT/public/about-us.html ... The issue I have is that I use a namespace for the admin area and the cont...
Written by Sean Behan on 06/17/2012
Using Your Partials in Your Liquid Templates
I'm working on a project that requires users/designers be allowed to edit the layout of their site. I'm using Liquid, Ruby templating system developed by the folks at shopify.com. Doing a little searching I found this great post http://giantrobots.though...
Written by Sean Behan on 06/17/2012
Custom Date Formats for Your Rails Application
If you use a consistent date format often in your Rails applciation, it is worth it to add the format to your application environment. You can do this by adding  it to the bottom of the config/environment.rb file. Time::DATE_FORMATS[:my_custom_format] ...
Written by Sean Behan on 06/17/2012
How to Install Ferret, the Full Text Search Engine with Your Rails Application
Ferret is a full text search engine based on the popular Lucene Engine, which is originally written for Java. There is a great tutorial available here: http://www.railsenvy.com/2007/2/19/acts-as-ferret-tutorial Assuming you have the plugin installed, act...
Written by Sean Behan on 06/17/2012
Starting the Rails Console in Production Mode
To specify which mode you'd like the rails console to boot up in, just provide the string without any flags. ./script/console production ./script/console test ./script/console development If you're on windows, remember the backslash "\" rather than for...
Written by Sean Behan on 06/17/2012
Sample Rails Database Config for MySQL
Sample Ruby on Rails database config file for connecting to mysql. production: adapter: mysql encoding: utf8 reconnect: false database: db_production pool: 5 username: db_user password: db_password #socket: /tmp/mysql.sock #this may vary ...
Written by Sean Behan on 06/17/2012
Rails Prototype JS and TinyMCE Autosave
TinyMCE is a nice little WYSIWYG for text processing online. It uses iFrames and Javascript callbacks to manipulate textarea form fields. Using it with Rails can be somewhat problematic if you want to set up an observer on a field that TinyMCE is managing...
Written by Sean Behan on 06/17/2012
Using jQuery and Prototype Javascript Together with jQuery.noConflict();
To use the jQuery javascript framework in a Rails application, that also uses the Prototype framework for the same application, you'll need to reassign the $() function for jQuery to another variable. This is very simple to do. Just make sure to include t...
Written by Sean Behan on 06/17/2012
Set Cron Job to Run Every Five Minutes for a Ruby on Rails Rake Task
First off you'll need to edit your cron file. Normally, the cron files are kept under /etc/cron.daily or /etc/cron.hourly but we can just use the command line tool, crontab and pass it the -e flag, so that we can edit the file without any fuss. sudo cron...
Written by Sean Behan on 06/17/2012
Setting up a new ubuntu server with apache2, php, ruby on rails, rubygems, mysql, and git
Here are a list of commands to get up and running with Apache2 with Phussion Passenger for Rails, PHP5, MySQL5, Ruby on Rails with Gems, and the source control software Git. I think that this is a pretty ideal environment for a development box and even pr...
Written by Sean Behan on 06/17/2012