Often, forms contain numerical fields for large numbers or monetary values. In such situations, it’s very useful to format these inputs with javascript (adding commas, dollar signs and so on). While the javascript is easy enough for this, rails does not like receiving commas or dollar signs in numerical fields.

Typically, one has two options to get rails to play nicely with this:

To add a workaround in the model to scrub out unacceptable characters.

To modify the submit button behavior to use javascript to scrub out those characters immediately prior to form submission.

Each of these strikes has always struck me as a bit cumbersome and intelligent. Surely this operation is common enough that a plugin of sorts must exist to handle this. …right?…….

Well I’m glad to tell you that after much searching, I’ve found a gem that does the trick. It’s called autonumeric-rails and it can be found on github or on rubygems.org. The Gem uses the autoNumeric jquery plugin to format a numerical input with commas, dollar signs or whatever else you might want.

Here’s how it works:

On top of the jquery plugin it makes the input agreeable with rails by creating a pair of inputs for each field: one visible, one invisible. The visible one is formatted using the standard jquery plugin, but the invisible one only contains the numerical value of the formatted visible field. Therefore, upon submitting a form, only the invisible, unformatted field is submitted.

So how do i use it?

Implementing the gem is very simple and it can be used with most of the options from the autoNumeric jquery plugin. Here’s how:

1. Add the plugin to your Gemfile and “bundle install”

2. Make sure that Jquery is “required” in your app and add autonumeric’s javascript file

//= require jquery
//= require autonumeric

3. Add the necessary data attribute to your input field.

This is where the autoNumeric jquery plugin site comes in handy, which has an interactive interface for building the appropriate data attribute based on your needs. Here are a couple examples.

4. That’s it, enjoy!

…But wait is it? Not quite, if you’re adding fields dynamically

Sometimes we want to add fields dynamically (once the page is already loaded). Many rails developers use the favorite cocoon gem for such a situation. The autoNumeric gem needs to be refreshed upon the addition of any fields in order for them to be formatted accordingly. It should also be pointed out that we must use the GEM’S javascript calls and not those of the jQuery plugin.

So for some nested fields in cocoon (as shown below), we add the following javascript.

I just upgraded to Kubuntu 16.04 beta and ran into a frustrating situation. My audio controls weren’t working; I tried my volume buttons, mute button and the little icon at the bottom (which happened to be highlighted in red).

When I went into Audio Volume Settings, all tabs had their own little version of “No _____ Detected”

So what to do?

After some troubleshooting with the fantastic help of “clivejo” in the #kubuntu freenode channel, it was finally determined that the settings that I carried over from my previous version of Kubuntu were causing the problem. Here’s how I solved it:

Remove directory ~/.config/.pulse/

Remove directory ~/.configh/.kmixrc

Do this for all user accounts that have been carried over (these configurations are user-specific)

With these four steps, you should regain control over your life again (or at least your audio).

Update:

You may find that the above temporarily solves the problem but upon the next bootup, it’s the same issue all over again. (That was the case with me.) Apparently there was another file somewhere in ~/.config/ left over from 14.04 that was screwing everything up. (Seemed it clashed with Plasma’s autologin and pulse audio got pinched in the middle.) Anyway, I just removed ~/.config/ entirely and started fresh. If that’s not an option for you, remove each file one by one and see which one’s the culprit. I just didn’t have the patience for that.

Global IDs have previously been a feature of rails with the help of a gem but as of Rails 4.2, it’s officially baked into the platform. I just recently used the new addition to create polymorphic associations within an app and I have to say, it’s fantastic. I did, however, find very little help out there for first-time users of this feature so here’s a few notes on how to do it.

What is a global id

Global Id is a library that produce Uniform Resource Identifiers (URIs) for any piece of data you’d like. They tend to look something like this:gid://name-of-app/Person/1

Global IDs have previously been a feature of rails with the help of a gem but as of Rails 4.2, it’s officially baked into the platform. I just recently used the new addition to create polymorphic associations within an app and I have to say, it’s fantastic. I did, however, find very little help out there for first-time users of this feature so here’s a few notes on how to do it.

What is a global id

Global Id is a library that produce Uniform Resource Identifiers (URIs) for any activerecord object. They tend to look something like this:gid://name-of-app/Person/1

So global IDs allow all of this information to be expressed in a single string very quickly. With a couple methods in the model we can use this to create a polymorphic association.

Cool, so how do I make this happen?

Let’s pretend that we want to set up an association to indicate whether a Stockholder is a Person or a Company.

First, we need to generate our stockholder model

We will need two columns: entity_id and entity_type. Just like in the standard polymorphic association, the latter stores the model name, the former stores the id within that model. We can generate both with a single line in our migration as follows:
class CreateStocholders < ActiveRecord::Migration
def change
create_table :optionees do |t|
t.date :grant_date
t.integer :shares_outstanding
t.belongs_to :option, index: true, foreign_key: true
t.references :entity, polymorphic: true, index: true
t.timestamps null: false
end
end
end

Next, we set up our models to indicate a polymorphic relationship

Company Model:class Company < ActiveRecord::Base
has_many :stockholders, as: :entity
end

So obviously, the exciting part here is the grouped_collection_select which will render a nice select menu with both companies AND people in it.

To break it down:

This is a grouped collection for a client

Over the models Company and Person

It will use all of the records in each model

and group them according to their model_name.

It will use the global_id to get the values

and display the email of each in the menu

The above will give you a grouped selector similar to the one shown below (note I used Org instead of Company, but you get the idea).

But you might rightly ask: “Why did you use email addresses and not names?” The tricky thing about these sorts of associations is that people have both a first name and a last name where companies just have a name. In order to get names to render in such a situation, you will need to use a lambda, which I’ll go over at the end of the post.

Making Global Ids work with Polymorphic Associations

If you’ve been keeping track though you should see that we have created two key problems for integrating the global id with polymorphic associations:

We need :entity_type as well as :entity_id

:entity_id needs to be an integer, not a string as is a global id.

So, to rephrase the situation, we need to break the global id into two pieces:

Its model name (a string)

Its id for that model (an integer)

Fortunately we don’t need to do this manually, rails polymorphic associations will store the model name and id automatically if it knows which object to reference. Therefore, we specify getter/setter methods to specify the object, using global ids.

Then just be sure to add :global_entity to your strong params and that’s it!

Using the lambda for names

Ok, often when doing polymorphic associations, one will use different pieces of information as keys for the two different models (after all, they’re not always going to have the same columns, right?) In such a case, we use a lambda, making our select like this:<%= f.grouped_collection_select :entity_id, [Company, Person], :all, :model_name, :to_global_id, lambda {|company_or_person_object| company_or_person_object.instance_of? Company? rescue company_or_person_object.fname + " " + company_or_person_object.lname rescue company_or_person_object.name}, label:"Stockholder", class: "names"%>

As an explanation of lambdas is a bit beyond the scope of this tutorial, I’ll just leave you with the example above. There is plenty of documentation available out there on the matter so if the lambda’s confusing, just google it up.

This problem had been a nuisance for me for a while, In the past, whenever I exited out of a window with lots of tabs, I’d get a nice warning message asking to confirm that I wanted to exit out of all pages.

This was fantastic because, on a qwerty keyboard, Q and W are right next to eachother. So, one slip of the finger turned a command to close a single tab into a command to close the whole darn window. But it seems that new versions of Firefox have disabled this warning by default. Today, with the help of Mardeg from the #firefox channel on irc.mozilla.org, I re-enabled it!

Here’s how:

Navigate to about:config?filter=browser.showQuitWarning in Firefox.

Then you will be confronted with some warning about voiding your warranty (see below) You really needn’t worry about this for the setting we’re changing. The warning applies to all navigations to about:config as some settings in there can indeed be precarious to change. This one is not.

Next you will be faced with the setting itself. Simply double click on it or right click to toggle it as true.

Finally close out of the tab and you’re done! The next time you exit out of firefox with multiple tabs, you will receive a warning.

Finally, if you want to do this for a separate admin model, simply replace User in the instructions above with Admin (provided of course that’s what your separate admin model is called; if not adjust accordingly).

I recently ran into a situation where I realized that the nice slick J-Query styling that I’d given to my select menus hadn’t carried over to my dynamically added fields. This is because, my javascript ran $('select').customSelect(); when the page loaded but at no point after. I therefore needed to figure out how to trigger that function once a dynamic field was added.

After quite a bit of searching around (on the order of hours) and several dead ends, I finally learned that cocoon has many different actions that can be used with on() In my case, all I needed was the following: $('#test').on("cocoon:after-insert", function(e, added_item){
added_item.find('select').customSelect();
});

One of the most confusing things about learning rails is in dealing with the Ruby Version Manager (RVM). Being a noob myself in the Ruby world, I wanted to give some clarification on stuff that I’d wondered at the outset regarding this tool.

RVM is basically a package manager (like apt for fellow linux users); it upgrades packages, downgrades them, allows you to switch out versions, etc. The big question on my mind was “why doesn’t ruby just use apt!?” Indeed, apt had served me well for php and it seemed to be a needless complication to use some other package manager for this language. “Why was Ruby so special that it needed its own package manager!?” I’m still not entirely sure of the rationale behind this, but my guess is that one of the things that makes Ruby so useful is its “Gems” (Rails is one of them), for which Ruby sort of acts as a package manager in itself. I figure this makes it much more important for users to have a more control over the versions of ruby that they’re using; either way RVM (and a few others like it) are the ways to manage ruby, so here are a few tips to make it work.

Note: These instructions might have an Ubuntu flavor to them here and there, because that’s what I use. For the most part, they’re universal though.

How can I upgrade to the latest version of ruby!?
There is no way to just upgrade ruby to the latest (whatever it happens to be). RVM seems to be all about giving control to YOU; it’s not going to make the choice for you. Here’s how I go about it.
First, I update my list of known stable ruby versionsrvm get master
Then, I check that updated list with:rvm list known
My output looked like this:

The real output was actually a lot longer as it gives you ALL the packages you can get with RVM but I cut it off (we’re not interested in JRuby or anything like that). Anyway, I checked out the list and saw that 2.2.0 was the latest so I entered:rvm install 2.2.0

Managing installed versions:
RVM also allows a user to have many “Rubies” installed (versions of ruby). To see which ones are installed:rvm list
Mine looks like this:

I’m using 2.2.0. If I wanted to use the version of 1.9.3 I have, I would typervm use ruby-1.9.3-p392
Also my default was 1.9.3. If I wanted to change the default to 2.2.0, I would enterrvm --default use 2.2.0

Upgrading RVM:
Every month or so it’s good to upgrade your rvm. To do so:rvm get stable