Select Drop Down in Simple_Form and Rails

I’m using the simple_form gem for forms in Rails and I needed to create a select (drop down) for a field.  In my case, it was for selecting US States, but this can be changed easily to whatever options you want to present by changing the options in the list.

In my user.rb model, I put the options (change these if your select is for something else than US States).

STATES =
[
[‘Alabama’, ‘AL’],
[‘Alaska’, ‘AK’],
[‘Arizona’, ‘AZ’],
[‘Arkansas’, ‘AR’],
[‘California’, ‘CA’],
[‘Colorado’, ‘CO’],
[‘Connecticut’, ‘CT’],
[‘Delaware’, ‘DE’],
[‘District of Columbia’, ‘DC’],
[‘Florida’, ‘FL’],
[‘Georgia’, ‘GA’],
[‘Hawaii’, ‘HI’],
[‘Idaho’, ‘ID’],
[‘Illinois’, ‘IL’],
[‘Indiana’, ‘IN’],
[‘Iowa’, ‘IA’],
[‘Kansas’, ‘KS’],
[‘Kentucky’, ‘KY’],
[‘Louisiana’, ‘LA’],
[‘Maine’, ‘ME’],
[‘Maryland’, ‘MD’],
[‘Massachusetts’, ‘MA’],
[‘Michigan’, ‘MI’],
[‘Minnesota’, ‘MN’],
[‘Mississippi’, ‘MS’],
[‘Missouri’, ‘MO’],
[‘Montana’, ‘MT’],
[‘Nebraska’, ‘NE’],
[‘Nevada’, ‘NV’],
[‘New Hampshire’, ‘NH’],
[‘New Jersey’, ‘NJ’],
[‘New Mexico’, ‘NM’],
[‘New York’, ‘NY’],
[‘North Carolina’, ‘NC’],
[‘North Dakota’, ‘ND’],
[‘Ohio’, ‘OH’],
[‘Oklahoma’, ‘OK’],
[‘Oregon’, ‘OR’],
[‘Pennsylvania’, ‘PA’],
[‘Puerto Rico’, ‘PR’],
[‘Rhode Island’, ‘RI’],
[‘South Carolina’, ‘SC’],
[‘South Dakota’, ‘SD’],
[‘Tennessee’, ‘TN’],
[‘Texas’, ‘TX’],
[‘Utah’, ‘UT’],
[‘Vermont’, ‘VT’],
[‘Virginia’, ‘VA’],
[‘Washington’, ‘WA’],
[‘West Virginia’, ‘WV’],
[‘Wisconsin’, ‘WI’],
[‘Wyoming’, ‘WY’]
]

Now in my simple_form, I put this:

<%= simple_form_for(@user) do |f| %>
<%= f.input :indiv_state, collection: User::STATES %>
<%= f.button :submit %>
<% end %>

Advertisements

Changing Column to Add Precision and Scale

If you have to add precision and scale to a decimal field, here’s a sample migration:

 

class ChangeScale < ActiveRecord::Migration
def self.up
change_column :shirts, :price, :decimal, precision: 5, scale: 2
end

def self.down
change_column :shirts, :price, :decimal, :precision, :scale
end
end

Creating a CSR on Windows

In order to create a SSL Certificate, you need to generate a CSR, or Certificate Signing Request.  I ran into some issues when generating one from Windows, so I wanted to document the steps that I took.

  1. went to http://slproweb.com/products/Win32OpenSSL.html to download Win32 OpenSSL v1.0.0k
  2. installed OpenSSL
  3. I received an error regarding C++ during the install, so I installed Visual C++ 2008 Redistributables from the same page I downloaded OpenSSL
  4. Run a command prompt (cmd.exe from the search box in the Windows start menu)
  5. When I tried to run an OpenSSL command, I received an error that a file could not be found (WARNING: CAN’T OPEN CONFIG FILE: /USR/LOCAL/SSL/OPENSSL.CNF)
  6. As noted here, you have to set the environmental variable by typing this into the cmd: set OPENSSL_CONF=c:\[PATH TO YOUR OPENSSL DIRECTORY]\bin\openssl.cfg
  7. navigate to the bin directory
  8. enter your openssl command to generate your csr.  For me, it was:
    openssl req -nodes -newkey rsa:2048 -keyout myserver.key -out myserver.csr
  9. this created key and csr files I could use for creating my SSL certificate

Error From Coffee Files

When I had to set up a new Windows PC with Rails, I ran in to a mysterious error where I couldn’t view my app because of purported problems with my .coffee.js files.  However, I knew there was no issue with the files since they were created during scaffolding and had only comments in them.  Furthermore, I was able to run the same code on my other PC fine, as is.

When I viewed the –trace there was nothing of note (except listing that the coffee files had issues with them).  When I tried to load the view, I did see this though “ExecJS::RuntimeError in Home#index”

Fortunately, I found the solution in this thread which explains that this is the result of Node.js not on a PC, or the path not set correctly in Windows environment variables.  I downloaded the Windows installer (.msi) file and installed Node.js and added the path to the PATH environment variable.  After I restarted my Rails server, the error went away.

Editing Hosts File as Administrator on Windows

I was having an issue editing the hosts file on Windows.  I was receiving an error saying that only the Administrator could edit the file since the etc directory was protected.  My user has Administrator access so I couldn’t understand what was going on.  I tried to change the settings on the etc directory and could not do that either.

So I ended up opening Wordpad (which I use to edit the hosts file), by right-clicking and selecting Run as Administrator.  Once I did that and opened my hosts file in Wordpad, I was able to save the file.

Ordering Query Results by Count

I had two models Article and Comment where I wanted to find out which articles had the most comments.

class Comment < ActiveRecord::Base
belongs_to :article
end

class Article < ActiveRecord::Base
has_many :comments
end

I found out how to do this using this Stack Overflow question which said create a new scope, such as this in the Article.rb model:

scope :top5,
select(“articles.id, OTHER_ATTRS_YOU_NEED, count(comments.id) AS comments_count”).
joins(:comments).
group(“articles.id”).
order(“comments_count DESC”).
limit(5)

Then in your controller, you can set the scope to a variable:

@most_popular = Article.top5

and in your view:

<% @most_popular.each do |most_popular| %>

# fields to display

<% end %>

How to Get Free Email Hosting for Your Domain

Google Apps no longer offers free email hosting for small businesses.  No worries – Microsoft offers free hosting through it’s new and improved Outlook and it’s extremely easy to set-up.

(1) go to https://domains.live.com and sign in (sign-up for a free live.com account if you don’t have one)

(2) add your domain

(3) open another browser window and log into your account with your domain name registrar (the website where you registered your domain (ie register.com or Go Daddy))

(4) in your domain name registrar account, go to where your domain name is managed and look for the place where you change nameservers.  once there, add the records listed in https://domains.live.com

(5) after you confirmed the settings are in place on https://domains.live.com, add a new mailbox (ie info for info@yourdomain.com)

(6) test the changes by sending an email to your new account (ie info@yourdomain.com) and then logging in via live.com