Automatically switch between SSL and non-SSL with Nginx+Unicorn+Rails

Scroll down for setup instructions. Or, read this bit about SSL in the real world first. SSL or Secure Socket Layer is a nice way to secure sensitive parts of your Rails application. It achieves to goals. Firstly is encrypts all traffic between you and the remote server. Consider the passwords and personal information you submit to websites. When unencrypted (using HTTP), all this data is sent over the internet for all to read.

Rails 3: Customized exception handling

Exceptions happen. There’s no way around that. But not all exceptions are created equally. For instance, a 404 “Not found” error can (and should) be handled correctly in your application. Let me give you an example of how to handle a ActiveRecord::RecordNotFound exception. Let’s assume you have an application that could show a user profile: # GET /p/:name def show @profile = Profile.find(params[:name]) end Now, it may happen that the :name paramater contains a value that cannot be found in our database, most likely because someone made a typo in the URL.

Testing Rails 3 scopes revisited

In my previous article I told you about how I like to tests my scope. There was a fair amount of criticism on that post and after considering it all (and hearing Corey Haines’ talk on Arrrrcamp last friday), I’m convinced it’s the wrong path. In essence Rails allows you to create a scope to generate a custom database query for you. Now, if you only test your configuration of Rails (as I did in my previous post), you don’t know if that scope works or not.

Properly testing Rails 3 scopes

The content of this post is no longer correct. Please read this article for details. Testing scopes has always felt a bit weird to me. Normally I’d do something like this: class Post < ActiveRecord::Base scope :published, where(:published => true) scope :latest, order("created_at DESC") end describe Post do context 'scopes' do before(:all) do @first = FactoryGirl.create(:post, :created_at => 1.day.ago, :published => true) @last = FactoryGirl.create(:post, :created_at => 4.day.ago, :published => false) end it "should only return published posts" do Post.

Rails 3 + Devise + Uploadify = No Flash Session Hacks

Uploadify is a great project to provide file uploads in your project. The problem is, it’s written in flash. Besides the point that it is flash, there’s something else that has been bothering me a lot: sessions. ~ The problem is like this. When a browsers opens a connection to your Rails app, it has a session. Normally, session information is stored a cookie that is sent with every request. This session also contains information needed for you to stay logged in as a particular user.

Why did error_messages_for disappear from Rails 3?

Today I learned that error_messages_for has disappear from Rails 3. When I tried using it I got the following deprecation warning: DEPRECATION WARNING: form.error_messages was removed from Rails and is now available as a plugin. What happened? Why was this pulled from Rails 3? ~ The reason error_messages_for was pulled from Rails 3 is a new guideline that says that nothing in Rails Core should dictate the look and feel of an app.