Skip to content

Add an ‘about’ method to your Rails app

Bob Roberts:

Man, after dealing with legacy code so many times, this is a great idea. Many times developers forget (or don’t care?) about the next developer that will work on the code someday. This would be good to throw into a rake task to create the full list of descriptions. Thanks for this!!

Originally posted on livingjim:

I end up using Ruby on Rails apps as tools within larger projects, not as an end to a web application need.  As such, my involvement with my Rails app may go months between those intensive times where my head is in the Rails app, and I forget many of the nuances and whys of the architecture.  So I recently built up a little extra goodness in my current project to help me when I return to it next time.

In short, I have added to each model a “description” method that just (now anyway) prints out my thinking about what the model is there for and how it is to be used, what caveats exist in the model, and so on.  So if my app has a Product model, I can work in the Rails console with it and issue a Product.description call to print out all that stored…

View original 166 more words

Benefits of Using Ruby on Rails Applications for your Business

Bob Roberts:

Nice write up even if it is self serving advertisement. I was not aware that Hulu is on Rails.

Originally posted on Offshore Ruby on Rails:

ImageRuby on Rails, also known as Rails is an Open source Web application framework that uses Ruby programming language which allows creating and developing pages with higher efficiency, more flexibility, moreover less time consuming.

Ruby on Rails has become one of the most popular and emerging programming language used for Web Application Development.

There are plethora of other programming languages available that can be used for creating web applications and CMS (Content Management System) like PHP, JAVA, ASP, Python, Perl, etc.

But Ruby on Rails stands out when it comes to Flexibility, Time, Cost, Security and Agility

Well, here are some of the considerable reasons why to choose Ruby on Rails over other Web Application Frameworks

Development Speed

Ruby on Rails allows features to be developed quickly by freeing programmers from repetitive coding. It emphasizes convention over configuration since it adopts agile development concepts. This allows Ruby on…

View original 427 more words

acts_as_taggable_on + select2 = Super Easy Tags in Rails

Bob Roberts:

Just what I wanted. Big fan of Chosen/Select2. Many thanks!

Originally posted on rcode5:

I’ve had two projects in the past little bit where I wanted user input tags for objects. I also wanted to have typed tags – tags of different categories applied to the same object. I found that acts_as_taggable_on (Rails gem) and select2 (jQuery plugin) work really nicely together and make this an easy task.

Let’s assume that we have an ActiveRecord model for a Post. Follow directions to install acts_as_taggable_on (add to Gemfile, initialize and run migration). Then add two different tag types to Post like this:


With this, the Post model is ready to store tags and topics.


To push this to the users, we want a nice UI for users to enter tags/topics. This is where Select2 comes in. In your form for adding/editing posts, add…

View original 91 more words

Avoid Common mistakes in Ruby on Rails Development

Bob Roberts:

Pretty nice write up. I never saw the find_each before. Will have to try that out :)

Originally posted on rubyonrailswebdevelopment:

Many programmers face similar problems over and over. So, here I am going to list out the common mistakes that can be avoided in Ruby on Rails Development .

1. Database column Indexing
At the beginning of programming , mostly during writing migrations, we forget to add indexes , which may or may not have any effect in initial stages of application, but will have drastic delay in fetching records from the database as the table size increases.

2. ActiveRecord transaction for saving multiple object
Whenever saving multiple objects in a single request, always use ActiveRecord::Base::transaction method to support atomicity. So, if any record has not saved the entire transaction, will get rolled back and nothing will be saved into the database.
Always add indexes to the column being used for searching records.

ActiveRecord::Base.transaction do
  @tasks.each do |task|!

3. Using delete_all instead of destroy_all
ActiveRecord::Base::delete_all or…

View original 1,031 more words

Ruby .try method

Originally posted on Gemfile:

This Ruby method returns nil rather than raising an exception when called on a non-existent object. 1

View original 167 more words

Pre-Caching a page in Rails

I recently had to figure out how to basically freeze a page in a rails app so that any changes to the dynamic data would not change the front facing view.

The app has a “web page” that is created dynamically and is then “published” to the web on a weekly basis. As this is always the same view, I needed to figure out how to save the view from each week. StackOverflow led me to the best option being to cache the page.

Easy enough, but what happens if nobody visits the page for one week? It is never cached. So after some googling I found that I could curl the page from the app and that cur (curl is a client to get documents/files from or send documents to a server, using any of the supported protocols (HTTP, HTTPS, FTP, GOPHER, DICT, TELNET, LDAP or FILE). The command is designed to work without user interaction or any kind of interactivity.) l would be sufficient enough to make the page cache.

I set the controller to caches_page on the show action. Then in the controller that performs the “publishing” I set it to clear the cached page. Nothing happened. I manually cleared the cache both in the project as well as in the browser and the page still displayed!

I finally found that the cached page was in my public folder and not the tmp folder. At first I thought that it was something that I set up incorrectly but then realized that it was how the page was setup in the first place. As this is a front facing page requiring no authentication it was named spaced in the controllers/public directory with the views in the /public directory. So that is why the cached pages were going to the public directory and not to the tmp directory as is the norm.

Now I had to manually find the cached copy and remove it.

As there really is not a need for the page to be instantly cached and no need to the user to wait for it, I moved it into a background job using Sidekiq.

So first I created a helper method to do some of the initial processing.  To the controller that does the publishing I need to add a couple of things:

include CacheHelper  ## this tells it to use the helper
update_web_cache     ## this is called in the action to publish

So when the user publishes it will call the CacheHelper:

## file cache_helper.rb
module CacheHelper

 def update_web_cache
   url = "#{ENV['HOSTNAME']}" + path_to_the_view_needed_to_be_cached

First I had to manually remove the cached view and I did so using FileUtils. Then as this is in the helper I have access to my model and therefor I can get the path to the view based on the model. Last, I tell Sidekiq to do it’s thing.

OK, so to this point we have removed the old cache but now we need to create the new cache. After a lot more googling the only way I could figure out how to curl the page was with a rake task. Further down the rabbit hole…

In the Sidekiq worker, I added the code to run the rake task. There were a few options to do this but I found the execute command to be the easiest and it worked.

## file cache_worker.rb
class CacheWorker
  include Sidekiq::Worker

  def perform(url)
   %x(bundle exec rake curl["#{url}"])

This then takes the url that it is passed and runs the rake task:

## file lib/tasks/curl.rake
desc "curl"
task :curl, [:url] => :environment do |task, args|
 system "curl #{args.url}"

Bottom line, created a background job that calls a rake task to curl a page.
Now, if there is any easier way to do this I sure would love to know.

Formatting Time and Date in Ruby on Rails Using strftime()

Featured Image -- 1123

Impersonate an user in Rails

Originally posted on Food for Thought:

I have been using Rick Olson’s restful_authentication plugin for quite some time now.

Recently I came across a requirement where the Site Admin was required to be able, to impersonate any user on the site.

Here’s how I achieved this:


When “Site Admin” logs-in on the site, I create two sessions namely:


If the Site Admin impersonates an user on the site,

replace the “session[:user_id]” with the id of the user we wish to impersonate.

So the Site Admin becomes the intended user.

Whenever Site Admin wants to stop impersonating,

replace “session[:user_id]” data with “session[:original_user_id]”

Site Admin regains original admin profile.


lib/authenticated_system.rb (Generated by restful_authentication plugin)

# Store the given user id in the session.
def current_user=(new_user)
session[:user_id] = new_user ? : nil
@current_user = new_user || false

I overloaded this method in my Application Controller:

def current_user=(new_user)

if new_user &&…

View original 207 more words

Ruby on Rails testing with Select2

I posted a while back about using the select2-rails gem together with the select2-capistrano gem. I recently upgraded an app and found that my tests that were using select2 were all failing. I did some googling and it seems that the select2-capistrano gem is no longer active.

Well, I went about it another way then.

First off, I do not add the class to each drop-down select but rather I do it globally using a collection_select_input.rb file in app/inputs/ like so:

    class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
      def input_html_classes

So when I started to have issues with capybara not being able to use the select2 drop-down my fix was to only use the global push if not in test environment:

    class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
      unless Rails.env.test?
        def input_html_classes

So now I have good old normal drop downs in test environment and the ever awesome select2 drop downs in production.

Detecting IE browser in Rails app

I have been working an app that is used by many users that are still on MS XP and using IE8.  It is a javascript heavy app and I have many issues in dealing with the cross-browser compatibility (IE) but today I found a great gem.

I was trying everything using CSS to hide a div in IE 8 to no avail.  And then Browser Gem to the rescue.

I added a simple check for IE 8 in my view:

    = if browser.ie8?

Thank you browser gem!!!