Blog · Articles tagged with "quicktip" · show all articles »

Autospace Bootstrap columns on small screens

A little trick I have been using in pretty much every design I worked on that was based on Bootstrap is to automatically add a margin between grid columns when they break/collapse, i.e. are displayed vertically instead of horizontally.

Read on

How to use Basic Authentication with the Ruby Rest-Client gem

I hope this will save someone the hour that it just took me to google, try out several StackOverflow answers and finally finding the solution in the source code (note to self: move "check the source code" to the top of the list of things to try next time).

Read on

Recalculate counter cache columns in Rails

Rails has this great feature called counter_cache. When you have a has_many/belongs_to relationship between two models, it basically lets you say that you want to save the number of associated objects on the has_many side. So if you have Post has many Comments, you can do post#size and no SQL query is executed, Rails simply just looks at the comments_count field of the post.

And - as with so many things in Rails - the beauty is that it just works out of the box, the way you would expect it to work. When you create or delete Comments, Rails keeps the comments_count fields of all associated Posts up to date. Well, most of the time. I have had the situation a couple of times where I ended up with a negative value which obviously doesn't make sense.

Read on

Load remote Rails links and forms in Bootstrap modals

As a heavy user of Bootstrap modals and Rails, I often want to use remote links or forms in a modal and load the result into the same modal. After doing this in several projects I worked on, I now copy and paste the helper directly into any new project I start that will use modals:

$(document).on 'ajax:success', '.modal [data-remote="true"]', (_, data) ->
      scrollTop: 0
      , 300

The trick is to specify only [data-remote="true"], not a[data-remote="true"] or form[data-remote="true"] so it works with remote links and forms. As you see, I also added a animation that scrolls to modal to the top after inserting the new content.

This helper works with Bootstrap 2.x and 3.x.

Read on

Get query parameter from the current URL with Javascript

When working with Javascript, you sometimes want to fetch the value of a query parameter from the current URL. I found multiple jQuery plugins to do this (in the horrible tradition of writing jQuery plugins for the most miniscule and simplest of tasks) but I ended up writing it myself in seven lines of Coffeescript (and I'm sure it could be compressed to one or two).

Update: as pointed out in the comments, my method has the flaw that it doesn't escape the query parameter it looks for, so it certain circumstances it might match the wrong parameter. If that concerns you, I would recommend to use this alternative method.

Check it out:

getQueryParam = (param) ->
  regex = new RegExp("[?&]#{encodeURIComponent(param)}=([^&]*)")
  match = regex.exec(
  if match?

Read on

How to drop your Postgres database with Rails 4

Update September 28 2016: There is now a gem that you can include in your app to use the technique detailed below.

When you use Postgres as your database with Rails and try to drop your database using rake db:drop, you might run into this annoying error:

PG::ObjectInUse: ERROR:  database "myapp_development" is being accessed by other users
DETAIL:  There is 1 other session using the database.
: DROP DATABASE IF EXISTS "myapp_development"

Read on