en en en en id en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en sk es en en en en en en en en fr en en en en en en en en en en en en en en en en en en en en en it en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en tr fr fa en en en en en en en en en en en en en en en en fa en en en en en en en en en en en en en en en en en en en en en en en en en fi en en en en de en en en en en en en en en en en en en en en en en en en hbs_lat en en en hbs_lat en en en en en en en en en en en en en en en en en en en en en en tr en en en en en en en en en en en en en en en en en en en en en en de en en en en en en en en en de en de en en en en en ceb en ceb en en en ceb en ceb en en en ceb en ceb en en en ceb en ceb en en en ceb en en en en en en en en en en en en en en en en en en en en en en en en en ko en en en en en en en fa en en en en en en en en ja en pl en en en en en en en en en en en en en en en en en en en en en es en en hbs_lat en en en en en en en et en en en ar en en en en en en en en en en en en en en en en en en en en en en en en en en en en en pt en en en en en en en en en en en en en en
As I stumble through the world of linux, I've decided to log new tricks I learn because I often forget. I decided to share the log because…. eh, why not.
Formatting Numerical Input with Rails
Kubuntu 16.04 GUI Audio Controls not working
Polymorphic Associations The Smart Way: Using Global Ids
Get Firefox to display warning message when exiting multiple tabs
jaehoo on Kubuntu 16.04 GUI Audio Contro…
BennyBeat on How to download a file from so…
fastrapidleech.com on How to download a file from so…
neanderslob on How to download a file from so…
Formatting Numerical Input with Rails
May 6, 2016May 6, 2016 neanderslobcocoon, form, formatting, number, rails, ruby on rails, stylingLeave a comment
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.
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
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
view raw _form.html.html.erb hosted with ❤ by GitHub
…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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
view raw _nested_fields.html.erb hosted with ❤ by GitHub
view raw additionaljs.js hosted with ❤ by GitHub
Now you’re actually done.  Happy coding!
Formatting Numerical Input with Rails
Kubuntu 16.04 GUI Audio Controls not working
April 21, 2016April 22, 2016 neanderslobaudio, controller, kde, kmix, kubuntu, pulseaudio, settings, sound, ubuntu, volume2 Comments
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:
Do this for all user accounts that have been carried over (these configurations are user-specific)
If the issue persists, reinstall pulseaudio: sudo apt-get install --reinstall pulseaudio
With these four steps, you should regain control over your life again (or at least your audio).
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.
The files that were causing trouble in my ~/.config/ were:
Kubuntu 16.04 GUI Audio Controls not working
Polymorphic Associations The Smart Way: Using Global Ids
November 3, 2015December 5, 2015 neanderslobglobal id, polymorphic associations, rails, ruby on railsLeave a comment
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:
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:
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
create_table :optionees do |t|
t.belongs_to :option, index: true, foreign_key: true
t.references :entity, polymorphic: true, index: true
Next, we set up our models to indicate a polymorphic relationship
class Company < ActiveRecord::Base
has_many :stockholders, as: :entity
class Person < ActiveRecord::Base
has_many :stockholders, as: :entity
class Stocholder < ActiveRecord::Base
In the above case, :entity is just catch-all field that represents people and companies. Of course, you can call yours whatever you’d like.
Now we can set up our form for the stockholder
I use simple_form but the principle is the same for other methods; adjust your syntax accordingly. Something like this.
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.
So, editing the Stockholder model again:
class Stocholder < ActiveRecord::Base
Then change the grouped_collection_select to:
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:
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.
Anyway, that’s it; you’re done!
Polymorphic Associations The Smart Way: Using Global Ids
Get Firefox to display warning message when exiting multiple tabs
June 1, 2015June 1, 2015 neanderslobexit, firefox, settings, tabs, warningLeave a comment
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!
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.
Get Firefox to display warning message when exiting multiple tabs
Cheatsheet for viewing commits/branches in git
May 8, 2015May 12, 2015 neanderslobgit, log, web-developmentLeave a comment
A couple notes on different log flags that I use for a couple applications
Only shows commits that are referred to by current state of branch
git log --graph --all --decorate --simplify-by-decoration
Shows all commits from all branches in a tree
git log --graph --all --decorate
A few notes on reading a branch.
If you are confronted with something like the following:
* commit 4589a323c3eddc4f488ea856c49db86d76bfe65f (Branch_5)
| Date: Tue Apr 14 01:52:17 2015 -0600
| * commit d168ac4d7ab7e80c85d0630dc00a67319293f6a7 (Branch_4)
| | Author: Neanderslob
| | Date: Tue Apr 28 15:01:00 2015 -0600
| | Commit 4
| * commit 06a6299ede5a1d6abf562b3a4999d6b6905dae80 (Branch_3/Branch_4)
| Date: Fri Apr 24 15:24:08 2015 -0600
* commit 2118bcb9d2c5fae313841fa66a5af6294cf8ba31 (Branch_2)
| Date: Sat Apr 4 02:49:06 2015 -0600
* commit 413697b0fbd36552613c51634080b419bf8ed6e9 (Branch_1)
| Date: Fri Mar 20 17:24:27 2015 -0600
The way you read it is:
Branch_2 comes off of Branch_1.
Branch_3 and Branch_4 came off of branch 2 at the same point
Branch_4 is ahead of Branch_3
Branch_5 came off of Branch_2 as well but after 3&4 did
Cheatsheet for viewing commits/branches in git
Adding a devise user or admin in a Heroku app
February 18, 2015February 18, 2015 neanderslobadd, devise, heroku, rails, ruby, ruby on rails, userLeave a comment
To add a devise user on a Heroku site we combine two commands
Command 1: With devise a person can enter the following into their rails console to create a user: User.new(:email => "user@name.com", :password => 'password', :password_confirmation => 'password')
Command 2: To run commands in the Heroku terminal, simply go into local directory of the corresponding app and type heroku run [whatever you want to run]
So if we put those together we get the following, assuming that your user model is in fact called User:
Change your directory to your app: cd ~/yourapp
Enter into the rails console on Heroku with the following: heroku run rails c
Run the create user code: U=User.new(:email => "user@name.com", :password => 'password', :password_confirmation => 'password')
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).
That’s it! You should be good to go!
Adding a devise user or admin in a Heroku app
Excluding directory from grep
February 14, 2015 neanderslobbash, exclude, grep, linux, notes, terminalLeave a comment
For some reason, I can never remember how to do this.
If you ever want to grep for something but leave out a particular directory, use the --exclude-dir=DIR
So if I wanted to search the current directory for the string img, but don’t want to look in the directories tmp or log.
grep -r --exclude-dir=log --exclude-dir=tmp "img" ./
Excluding directory from grep
Styling Fields Dynamically Added With Cocoon
February 5, 2015 neanderslobcocoon, dynamic, fields, jQuery, nested, rails, ruby, ruby on railsLeave a comment
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:
And I was good to go.
Styling Fields Dynamically Added With Cocoon
A beginners look at RVM: Installs, Upgrades and the rest
January 27, 2015February 25, 2016 neanderslobinstall, ruby, ruby on rails, rvm, update, upgrade1 Comment
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.
\curl -sSL https://get.rvm.io | bash -s stable --ruby
To check the version of ruby:
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 versions
Then, I check that updated list with:
My output looked like this:
[ruby-]1.8.7[-head] # security released on head
# for forks use: rvm install ruby-head- --url https://github.com/github/ruby.git --branch 2.1
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 also allows a user to have many “Rubies” installed (versions of ruby).  To see which ones are installed:
Mine looks like this:
ruby-1.9.2-p320 [ x86_64 ]
* ruby-1.9.3-p392 [ x86_64 ]
=> ruby-2.2.0 [ x86_64 ]
# => – current
# =* – current && default
# * – default
I’m using 2.2.0. If I wanted to use the version of 1.9.3 I have, I would type
Also my default was 1.9.3. If I wanted to change the default to 2.2.0, I would enter
rvm --default use 2.2.0
Every month or so it’s good to upgrade your rvm. To do so:
A beginners look at RVM: Installs, Upgrades and the rest
January 22, 2015January 22, 2015 neanderslobcheat sheet, cheatsheet, controller, current_user, edit, index, new, posts, rails, ruby on railsLeave a comment
Common in index method
Get all posts and save them to @post, ordered by when they were creating in descending order:
@post = Post.all.order("created_at DESC")
Get all posts by a particular user and save them to @post, ordered by when they were creating in descending order:
@post = current_user.posts("created_at DESC")
Common in new method
Create a new post for the current user when that user has_many posts:
Create a new post for the current user when that user only has_one post:
To authorize which users can access what
See my post here.
Create a free website or blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.