Select Drop Down in Simple_Form and Rails

I’m using the simple_form gem for forms in Rails and I needed to create a select (drop down) for a field.  In my case, it was for selecting US States, but this can be changed easily to whatever options you want to present by changing the options in the list.

In my user.rb model, I put the options (change these if your select is for something else than US States).

[‘Alabama’, ‘AL’],
[‘Alaska’, ‘AK’],
[‘Arizona’, ‘AZ’],
[‘Arkansas’, ‘AR’],
[‘California’, ‘CA’],
[‘Colorado’, ‘CO’],
[‘Connecticut’, ‘CT’],
[‘Delaware’, ‘DE’],
[‘District of Columbia’, ‘DC’],
[‘Florida’, ‘FL’],
[‘Georgia’, ‘GA’],
[‘Hawaii’, ‘HI’],
[‘Idaho’, ‘ID’],
[‘Illinois’, ‘IL’],
[‘Indiana’, ‘IN’],
[‘Iowa’, ‘IA’],
[‘Kansas’, ‘KS’],
[‘Kentucky’, ‘KY’],
[‘Louisiana’, ‘LA’],
[‘Maine’, ‘ME’],
[‘Maryland’, ‘MD’],
[‘Massachusetts’, ‘MA’],
[‘Michigan’, ‘MI’],
[‘Minnesota’, ‘MN’],
[‘Mississippi’, ‘MS’],
[‘Missouri’, ‘MO’],
[‘Montana’, ‘MT’],
[‘Nebraska’, ‘NE’],
[‘Nevada’, ‘NV’],
[‘New Hampshire’, ‘NH’],
[‘New Jersey’, ‘NJ’],
[‘New Mexico’, ‘NM’],
[‘New York’, ‘NY’],
[‘North Carolina’, ‘NC’],
[‘North Dakota’, ‘ND’],
[‘Ohio’, ‘OH’],
[‘Oklahoma’, ‘OK’],
[‘Oregon’, ‘OR’],
[‘Pennsylvania’, ‘PA’],
[‘Puerto Rico’, ‘PR’],
[‘Rhode Island’, ‘RI’],
[‘South Carolina’, ‘SC’],
[‘South Dakota’, ‘SD’],
[‘Tennessee’, ‘TN’],
[‘Texas’, ‘TX’],
[‘Utah’, ‘UT’],
[‘Vermont’, ‘VT’],
[‘Virginia’, ‘VA’],
[‘Washington’, ‘WA’],
[‘West Virginia’, ‘WV’],
[‘Wisconsin’, ‘WI’],
[‘Wyoming’, ‘WY’]

Now in my simple_form, I put this:

<%= simple_form_for(@user) do |f| %>
<%= f.input :indiv_state, collection: User::STATES %>
<%= f.button :submit %>
<% end %>

Fixing “SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed” Error on Windows

When I tried to implement OmniAuth, I received this error “SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed”.  Essentially, I was getting this error because the code was searching for an SSL certificate on my PC.

I didn’t have a SSL certificate for my PC, fortunately I found a work around to fix this.

Tracking Conversions in Google Analytics for Devise User Sign-Ups

I wanted to track user sign-ups as a goal, while using the Devise gem.  The Devise gem redirects users to the root path as a default, which I wanted to keep.  The problem with that is that I didn’t want to every visit to the root page as a conversion, only the ones that were a redirect after the user registered.

Here’s what I ended up doing:

In the users#registrations_controller.rb, I added this flash:

flash[:user_signup] = true

then in the root path view, I added:

<% if flash[:user_signup] == true %>
<%= render :partial => “shared/google_analytics_user_sign_up” %>  # edited google analytics code
<% else %>
<%= render :partial => “shared/google_analytics” %> # regular google analytics code
<% end %>

In the google_analytics_user_sign_up partial, I have this:

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga(‘create’, ‘UA-99999999-1’, ‘’);
ga(‘send’, ‘pageview’, ‘/special/path’);
<!– Google Analytics end –>

In Google Analytics, add it as a goal, in the destination add:


You can name the path whatever you want, as long as they match in the code and in Google Analytics.  Just make sure that it’s unique, it’s not the path of another url someone might go to.

Creating a Blog for Rails App

I wanted to create a blog for my Rails app and investigated ways to do this.  You can always start a free blog on Blogger, WordPress or Tumblr, but I wanted the blog to be a part of my domain.

There are gems to help do this.  I found the Bloggy and PostMarkdown gems.  Although they seem to integrate well with Rails, I wanted to use an existing CMS platform so I didn’t have to mess with the code of my app and to make creating posts easier. Adding markdown files to my app, generating views, and then pushing every time I wanted to make a blog update deterred me.

Instead I found a way to use WordPress for my blog here.  I liked the I could leverage WordPress to make posting easier, as well as the ability to use plug-ins people have built for assisting with SEO.  The nice thing about this set-up is that the blog uses your domain (i.e. address and it doesn’t drain your resources because it is running as a separate (free) Heroku app.

The biggest downsides I’ve found are that you need a place to store your files for your posts (S3) since you can’t write to a Heroku file system when posting through WordPress.  Also, you need to push files, such as templates and plug-ins.


ActiveAdmin: Turning Hashes/ActiveRecord Hashes to Field Values

After I installed ActiveAdmin gem, everything worked great except I had an issue where the dropdown menus were showing ActiveRecord hashes like this:


Instead of showing a name like the other fields.  It seems that the default is to show a field called “name” in the dropdown menus in ActiveAdmin, and if you don’t have such a field it shows the ActiveRecord instead.  Changing that was easy, after I found this StackOverflow question.

All you have to do is go into the model and add a method for to_s, like this:

def to_s
# field name you want to show in the drop down

for me, I put:

def to_s

Whenever Gem Without Capistrano

The Whenever gem simplifies the process of using scheduled tasks without learning CRON.  Stated another way, it allows you to schedule CRON jobs without knowing CRON’s abbreviations and syntax by allowing you to enter your scheduled tasks in a schedule.rb file using straightforward language.

When you deploy using Capistrano you can put code in your deploy.rb file so that Whenever tells CRON of any scheduling changes.

However, if you’re like me, and not deploying with Capistrano than you just have to remember to use this command in Linux/Ubuntu:

whenever -i

To confirm that your task was scheduled, you can enter:

crontab -l

This should indicate that your tasks in schedule.rb were scheduled to run via CRON.

Creating a Sitemap Automagically for Rails App

Sitemaps help in making sure that your app is properly indexed, so it would help to automatically have them generated (and updated) so a search engine knows when a new web page is created, or an old one updated.  Luckily, there’s a gem, SitemapGenerator that makes it extremely easy to do this.

All you have to do  in Rails 3 is:

  1. add: gem ‘sitemap_generator’ to your gemfile
  2. run “rake sitemap:install” which creates config/sitemap.rb
  3. customize sitemap.rb

A simple example (where “Content” is a model name)

SitemapGenerator::Sitemap.default_host = ""
SitemapGenerator::Sitemap.create do
  add '/contact_us'
  Content.find_each do |content|
    add content_path(content), :lastmod => content.updated_at

4. run “rake sitemap:refresh”

5.  change your robots.txt file (if you have one) to include this line (so search engines can find it easy):


6.  if you’re using Heroku, like me, you’ll have to take extra steps, as in this example, to write the files to somewhere else since Heroku is read-only.

7.  Also, if you’re using Heroku, you may want to use Heroku Scheduler to automatically refresh the sitemap by adding this task:

$ rake sitemap:refresh

8.  Lastly, to make sure Google indexes your site better, you’ll want to enter the path to your sitemap in a Google Webmaster Tools account for your site – the same as the address in step 5, unless you are using Heroku or another read-only system (and the sitemap is stored on another domain).  In this case, since Google Webmaster tools only accepts url’s from your domain (for the sitemap location), you’ll have to re-route them to the external location.  To do that you can make a simple controller like this example from Bill Rowell or you can use the rack-rewite gem.