Archive image

Posts Tagged ‘rails’

How to Save Multiple Checkbox Values to a Database in Rails

Imagine you have a form in your Rails app which is backed by an ActiveRecord model. In this form there are a bunch of checkboxes, the values of which you want to persist to your database. How do you go about handling this scenario?

Read more…

Nested attributes and updating without a password in Devise

I’ve spent all day trying to get Devise and nested attributes to play nicely together. This and giving the user the ability to update parts of their profile without providing a password proved kind of tricky. Here’s how I got things working.

Read more…

Authentication with Devise and cancancan in Rails 4.2

This is a beginner level tutorial on how to set up authentication (verifying who you are) and authorization (what you are permitted to do) using Ruby 2.2, Rails 4.2 and two popular Ruby gems: Devise and cancancan.

Read more…

How to setup Rails with PostgreSQL on Linux Mint

Rails is database agnostic, meaning that it can talk to different databases without more than configuration changes. Here’s how to install PostgreSQL on Linux Mint 16 and configure it for use with Rails.

Read more…

Resurrecting a Rails 2.x app

This morning I had to give a client an estimate for some work they wanted doing on a Rails 2.1.2 app.

The work itself wasn’t overly complicated, but getting a system up and running using Ruby 1.8 and Rails 2.1 proved to be somewhat of a challenge.

Here’s how I did it.

Read more…

Setting a fallback font in Prawn

At work, one of the applicants to our programme submitted parts of her application in Russian (despite it being an English speaking programme).

The database could handle this fine, as it stores entries using the UTF-8 character set, but the PDF generation part of our application refused to cooperate (just displaying a bunch of underscores). 

We use the Prawn library to generate our PDFs, so I set about finding a way to make Prawn play nice with the Cyrillic characters.

Read more…

Prevent double data submission in Rails 3

I was recently working on an application, that lets people from all over the world apply for a programme at the university I work at.

The application works well and does what it should, but we were seeing quite a lot of double submissions, that is to say, the same person applying multiple times in short succession.

Here’s how I fixed that.

Read more…

Custom routes with resources in rails 3

The problem: you have a RESTful resource with default routes. The form to create a new item is located at When you submit the form with valid input, a new item is created and that works fine.

However, when you submit the form with invalid input, the controller re-renders the form and the URL is changed to This is potentially confusing for end users.

How can we avoid this?

Read more…

Using virtual attributes in Rails 3

In Rails, virtual attributes allow you to create form fields that do not map directly to the database.

This can be useful in a variety of situations and can help you customize your interface to make it more intuitive and user-friendly.

In this short tutorial I will show you how to create three text fields to allow a user to enter a date (day, month, year), then combine the values entered into these ‘virtual’ fields and save the new value to the database.

Read more…

Inline error messages with Rails 3

I was recently tasked with converting a Rails app from Rails 2.3.x to 3.2.x.

“Shouldn’t be too difficult” I thought, but one of the first things that came out and bit me is that the error_message_on helper method, which was previously used to display error messages next to the form fields that had caused them, has been deprecated.

It took me a little while to figure out how to reinstate this functionality. Here’s how I did it.

Read more…