Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “ruby on rails

NoMethodError undefined method `[]’ for “image”:Sass::Script::Value::String

When upgrading my Rails app to 4 (from 3), I was getting the error:

NoMethodError undefined method `[]’ for “image”:Sass::Script::Value::String

Eventually I found out that the asset-path / asset-url methods changed to only accept one argument.

For example,

asset-url(‘profile.png’, image)

now should be:

asset-url(‘profile.png’)

In my particular case, this code was in some of my css files.

Advertisements

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 %>


Checking Instance Variable in RSpec After Update

I was having trouble getting one of my RSpecs to pass which I knew should be passing.  The problem was that when I did the check, the instance variable still held the old value.

For example:

it “should update status to in progress” do

  issue = FactoryGirl.create(:issue, status: “new”)

  Issue.method_to_update_status

  issue.status.should eq “in progress”

end

I knew  that the status was updating, but for some reason the test kept failing.  I found the answer with this SO question.

In order to get the issue record to reflect the new value, I had to add reload after it.

it “should update status to in progress” do

  issue = FactoryGirl.create(:issue, status: “new”)

  Issue.method_to_update_status

  issue.reload.status.should eq “in progress”

end


Setting Default Values for Rails Fields

For some fields, you may want to set default values for when a new record is created.  For example, if you want to set a flag/boolean field to true or false when a new record is created.  An easy way to do this is through a migration.

If you are just creating the field, you can do:

def change
add_column :table_name, :field_name, :field_type, :default => 0
add_column :table_name, :field_name, :field_type, :default => true
end

To edit an existing field:

def up
change_column :table_name, :field_name, :boolean, :default => true
end


Using Route 53 to Map your Domains

Route 53 is an Amazon Web Service (AWS) tool for routing domain names to your AWS servers.

It’s very easy to do:

(1) log in to your Amazon Web Services account and go to the AWS Management Console

(2) go to Services > Route 53

(3) in Route 53, click the “Create Hosted Zone” button

(4) once your zone has created, you should have four “Delegation Sets”.  The Delegation Sets are the addresses that you will need to point your domain name servers to.  You do this typically by logging into your account with the company that you registered your domain with (ie Yahoo or Go Daddy), and then for this domain finding the section to edit or change your name servers.

(5) after you have updated your name servers with the domain registering company, go back to the AWS Management Console.  In the console, go to Services > EC2.

(6) If you haven’t already, you will probably want to either create a static IP or elastic load balancing, and have whichever you create point to your EC2 instance

(7) if you are using an elastic IP, it will have an IP associated with it.  Copy that and go back to your hosted zone in Route 53

(8) For your hosted zone, click “Go to Record Sets”

(9) Next, click “Create Record Sets”

(10) select:

    • IPv4 for the type
    • if you are using elastic load balancing:
      • click “yes” for alias
      • select your load balancer from the menu that appears
    • if you are using elastic IP
      • leave “no” for alias
      • enter your IP in the “value” box
    • save

(11) Most likely, you’ll want to do this at least twice – once for your domain with “www” and one without – that way people can reach your app if they type www and if they don’t.

Your domain name change will take awhile to propagate, but once it does, your domain will be pointing to your AWS server(s) now.


Setting Up SSL on AWS Bitnami Instance of Ruby on Rails (Apache)

Setting up SSL on the Bitnami instance of Ruby on Rails is easy.

  1. First, you’ll want to make sure that you’ve purchased your SSL certificates for your domain
  2. Next, log in to your EC2 instance
  3. place the .key and .crt files you have for your SSL certificate in your apps’s root directory
  4. go to your app’s root and move the files to the correct directory with

mv yourcrt.crt /opt/bitnami/apache2/conf/extra/yourcrt.crt

mv yourkey.key /opt/bitnami/apache2/conf/extra/yourkey.key

       5.  the httpd.conf file that the instance comes with already points to another file for the ssl configuration, so we’ll want to edit that file that is being used.

6.  enter “cd  /opt/bitnami/apache2/conf/extra”

7.  enter “pico httpd-ssl.conf” to edit the ssl configuration

8.  in the file (CNTL + X to save when you are done):

    • change the “DocumentRoot” to “home/bitnami/apps/yourapp/public” or where ever your app is
    • change the “ServerName” to http://www.yourdomain.com:443
    • change the “ServerAdmin” to your email
    • change the “SSLCertificateFile” to “/opt/bitnami/apache2/conf/extra/yourcrt.crt”
    • change the “SSLCertificateKeyFile” to “/opt/bitnami/apache2/conf/extra/yourkey.key”

9.  stop and restart your server

apachectl -k stop

apachectl -k graceful

Your app should now be successfully set-up for SSL.


Using AWS Route 53 for Domain Name and Mailserver Routing

https://forums.aws.amazon.com/thread.jspa?messageID=315119