Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “rails 3

Infinite Loop with Force_SSL

I ran into an issue where I keep getting infinite loops on views with force_ssl.  I’m using Nginx and found that I needed to adjust the config.

For me the settings were in /etc/nginx/sites-enabled/default.

I needed to add this in the @location part:

proxy_set_header X_FORWARDED_PROTO $scheme;

Here's how my file ended up looking:

server {
    listen   443;
    ssl on;
    ssl_certificate     /etc/ssl/xyz.crt;
    listen   80;
    root /home/rails/public;
    server_name _;
    index index.htm index.html;

    location / {
            try_files $uri/index.html $uri.html $uri @app;
    }

#       location ~* ^.+\.     (jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rt    f|js|mp3|flv|mpeg|avi)$ {
location ~* ^.+\.    (jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp    3|flv|mpeg|avi)$ {
                    try_files $uri @app;
            }

     location @app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;
            proxy_set_header X_FORWARDED_PROTO $scheme;
}}

RSpecs

It has not been a picnic using RSpecs.  One of the steeper learning curves I’ve found in Rails.  Here are some random tidbits I’ve found:

(1)  the rspec command doesn’t get recognized in Windows for me.  So I have to do: rake spec:controllers (or whichever folder)

(2) to run a single test, from the root directory, do:

bundle exec rspec spec/(path to file).rb

ie

bundle exec rspec spec/controllers/user_controller_spec.rb


Caching Rails 3 on Heroku

To learn about caching, I used a few different resources.

The three basic types of caching are:

  • page caching – mainly for static pages, it will not work if any filters are applied to the controller of the page (ie authorizations)
  • action caching – typically what you’d used to cache a page if they have filters
  • fragment caching – allows for caching of blocks of html on a page (if you can’t cache the whole page) or for partials

Since I’m on Heroku, the first place to learn was Heroku’s documentation.  It’s important to read the Heroku docs first because some standard Rails functions work differently on Heroku’ platform.  Mainly, for page caching (uses different syntax than standard rails) and action caching (mostly dependent on memcache, so you need to set your app up accordingly).

Overview of caching on Heroku
http://devcenter.heroku.com/articles/caching-strategies

http://devcenter.heroku.com/articles/building-a-rails-3-application-with-the-memcache-addon

There’s also a Rails Guide on Caching (just remember to ignore page caching part):
http://guides.rubyonrails.org/caching_with_rails.html

However, the most extensive (and useful) one for me was from Adam Hawkins at (especially for fragment caching):
http://broadcastingadam.com/2011/05/advanced_caching_in_rails

I also needed to learn about sweepers, in order to observe for changes that would impact another controller’s pages.  This Stack Overflow post helped me learn to put the sweeper in with the models and to ensure that I updated the correct controller:
http://stackoverflow.com/questions/2247410/action-caching-is-not-expiring-correctly-even-when-i-can-see-its-being-called


Links for using :remote for Ajax and Javascript in Rails 3

Some good articles on how to use Ajax/Javascript with Rails 3:

http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/

http://rails.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M002239

http://railscasts.com/episodes/294-playing-with-pjax


Subdomains

here are some tutorials on making subdomains work in Rails 3.0

http://railscasts.com/episodes/221-subdomains-in-rails-3

 

http://bcardarella.com/post/716951242/custom-subdomains-in-rails-3

 

if you want to test this locally, you need to edit your hosts file:

 

for windows users: http://www.ehow.com/how_5225562_edit-windows-hosts-file.html