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 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 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 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 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
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
$('div#dynamic').on('cocoon:after-insert', function(e, insertedItem) {
view raw additionaljs.js hosted with ❤ by GitHub
Now you’re actually done.  Happy coding!
Formatting Numerical Input with Rails
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
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
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
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.
Setting Up Mailer Using Devise For Forgot Password
January 1, 2015January 12, 2015 neanderslobdevise, password, rails, ruby on railsLeave a comment
Really cleared up my confusion about sending email in both development and production environments.
Ruby on Rails Help
In this tutorial I will show you how to set up the mailer for the forgot password feature in Devise. In the tutorial I will be setting up a Gmail account and I will show you how its done using local environment variables. I will also be using Heroku and Foreman to set up environment variables.
I am using Rails 4 and Devise 3 for this tutorial.
Seting Up Development Environment
First we will set up the development mailer for use on your local machine. In “config/environments/development.rb” you should already have included
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
when you installed devise.
Next you should turn on the option to raise an exception if there is an error when sending an email. You can do this by including
in the same file. Next we will add the email delivery method. You should leave the values as…
View original post 672 more words
Setting Up Mailer Using Devise For Forgot Password
Notes: Adding themes to rails app
December 11, 2014December 11, 2014 neanderslobnotes, rails, web-developmentLeave a comment
To add themes to a rails app
Drop respective css and javascript files into appropriate directory under vendor/assets/.
Note: Files should have unique name (I name them after the theme so flatty-theme/css/style.css simply becomes flatty.css.
Add the following code to your config/application.rb under # add custom validators path in the Application class
Adjust whatever views you’re using to use the appropriate classes in your theme
Notes: Adding themes to rails app
How to Override and Customize the Devise Controller in Rails
December 10, 2014December 10, 2014 neanderslobauthentication, controller, devise, mvc, rails, web-developmentLeave a comment
Was an absolute lifesaver when I was trying to add additional registration information to my sign-up page.
How I Learned Ruby on Rails
Judging by the number of different StackOverflow questions, there are a lot of people trying to do this, and a lot of confusion. Here is how I did it, and hopefully it helps you.
I have a User and a Verifier model.  What I want to do is create a new Verifier every time I create a new user, and pass in the user.id for the User into the verifier.user_id so that they are mapped together.
In order to do this I want to not really override but add additional functionality to the existing devise controller that handles when new users are created (and destroyed).  So I need to access the RegistrationsController#Create function in devise.
First thing is to create a new folder in the ‘app/controllers‘ folder where we can put my custom controller.  I called mine ‘app/controllers/my_devise‘.  Then create a new file in this folder…
View original post 534 more words
How to Override and Customize the Devise Controller in Rails
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.