Six Ruby on Rails Tips & Tricks
Six Ruby on Rails Tips & Tricks to Make Your Life Better

Since I started with Ruby on Rails a few months ago here with ThreeTwelve Creative, I've run into a few problems while working on some of our Ruby on Rails custom web applications that didn't have overly obvious solutions to me as a newcomer. Looking back, I wish I had known about these tips before I either ignored the problem or solved it with a bad or difficult method.
1. Sanitize:
We have a couple of clients with fully custom rails apps that generate their own content. Much like you would expect from Wordpress or a standard CMS, they are fully permitted to insert their own HTML. However, that became a problem when I was truncating the content to display on an index or search results page. Using a simple
.html_safe
was in many cases opening a <div> which was never closed (because I truncated it), resulting in something like
Website Nightmares with Chef Jesse.
However, they still wanted HTML included on the index--in this case, much of the content started with an image, and they wanted it shown on the index.
It's a pretty simple fix, though. Using sanitize, you can selectively include or exclude certain html tags.
<%=truncate((sanitize blog.body, :tags => %w(img)), :length => 600)%>
Make sure to run truncate on the outside of sanitize to ensure that you get pretty close to your expected length.
2. Slugs:
Here's a one-liner to create slugs pretty reliably. It's not perfect, but definitely passable:
def create_slug
self.slug = self.title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
3. Helper Methods:
Making a method available to a view is fairly simple. Just declare it a helper method.
def admin?
#logic to check if admin user?
end
helper_method :admin?
4. Easy rename:
Sometimes, for whatever reason, your URL structure needs to change. A for-instance we ran across was a model named 'posts,' using the default controller like /posts/title-slug. The client wanted the structure to be /blog/title-slug or similar.
Changing the model name would be tiresome and nearly impossible to do perfectly. It can be done more simply with this in the routes:
resources :blog, :controller => 'posts'
You will have to fix your links. Hopefully you're using the
link_to URL helper method throughout the project. In that case, the fix is done in one place. Otherwise, you'll have to find each place you create a link and change it. On the upside, forgetting one won't break anything, as /posts in my example would still work.
5. Log Level:
The verbose output of WEBrick is adjustable. Sometimes when I log something for debug purposes, I can't scroll back far enough in the output to see it, for all the SQL queries (and, recently, content-length errors that 1.9.3 introduced and I'm too lazy to fix). It's partly a function of my sub-par shell (MINGW32 on Windows), but it's solved with a line in my environment.rb file:
config.log_level = :error
This works well unless I need to debug the SQL queries being generated by ActiveRecord, at which point I set :error back to :debug. FWIW, the different levels available are :debug, :info, :warn, :error, and :fatal, from most to least verbose.
6. Heroku db:push:
If you're trying to push a database, you might have run into some trouble. I've had various degrees of trouble personally, and in the end the problem was that I was using the latest version of ruby. There's an issue with taps/heroku/ruby 1.9.3 at the time of writing (2/15/2013). Using 1.9.2 is simple enough on Mac or Linux using RVM, and that's the quick-fix.
Here's hoping I save someone a few minutes! If you liked this post, visit me on
Google+ or follow me on
Twitter or
Facebook