• Four Steps to Turbocharge Rails + AJAX Development with Nginx and Foreman

    If you’re developing a chatty AJAX app on Rails and using a single mongrel to run it on your workstation, you probably are a bit annoyed with delays waiting for requests to be fulfilled one-at-a-time. So, here I’ll walk through the steps to run your own “cluster” on your OS X workstation using Foreman and Nginx.

    In the course of building the frontend application for Earbits, I’ve been constantly annoyed with the responsiveness of my local development server. As a pretty complex AJAX application, there are lots of little (and some bigger) calls to the Rails backend APIs to do lots of potentially slow things. This means that there are lots of calls that are originating in the browser and being served by one mongrel, so the server ends up handling requests too slowly. Which leads to me being frustrated. Which leads to me finding a nice solution to the problem.

    (more…)


  • Authlogic Perishable Token changes and breaks emails

    Overall, I think Authlogic is pretty great.  It’s saved me a bunch of time that I don’t have to worry about user creation and user session maintenance.  But this one little doozy was causing some issues when sending out one-time-use emails like invites or password resets.

    The perishable_token is the suggested way to send out secured one-time-use emails because it’s updated every time the User model is updated (like, when the user logs in) so the email can’t be re-used to reset the password again for instance.  I’m using it in some pretty strange ways to handle proxy users (users who haven’t actually registered yet on our site), but this issue also is present for the password-reset case.

    I noticed that some users were getting rejected after clicking the link in the email saying that the perishable token was no longer valid. “That can’t be right,” I thought to myself – because I know for a fact that some of those users hadn’t logged in.  But their token was still invalid. So after a little bit of troubleshooting, I realized the issue….

    The token is updated every time the User object is saved. Not just when they login.  And I have jobs running in the background to update the users with external data pulled from various APIs.  So my job was resetting the perishable_token for every user it touched.  Ok, so how did I fix the issue?  Simple really – I just took control of the maintenance of the perishable_token away from Authlogic and handled it myself.

    (more…)


  • Tip: Using vim with Rails Apps

    I’ve been trying a few different editors for working with Rails, including TextMate, Aptana and my trusty vim. I was leaning towards TextMate (bought a license too) but since I also develop on Ubuntu and would like to standardize tools to a certain extent, I decided to see if I could supercharge vim. The answer is a resounding YES.

    This plugin is awesome. Because it is aware of the Rails app structure, you can do cool things like jumping between files using commands like :Rcontroller User which will load the UserController or :Rview places/new which loads /app/views/places/new.html.erb. And, when your cursor is over a reference like a has_many line in a model if you key in gf, it will jump to the file where that reference is defined.

    Or, if you’re looking at specific action in a controller, the command :R will jump you to the view for that action (and vice versa). It even supports refactoring and neato stuff like that. These are just a few examples, try it out and read the documentation to see how powerful it is!


  • Aha! The trick to getting custom named routes!

    It was frustrating me that I had to hardcode strings like /activate/#{user.activation_token} when using link_to and other constructs with wacky custom routes (because they wouldn’t auto-generate a named route). The trick is to add :as => 'routename' to your routes.rb match statement. This will then expose routename_url to the rest of your app so you can do routename_url(user.activation_token) to get the proper url.

    It’s easy when you know how… Also, $ rake routes is essential to understanding the magic under the hood. Use it!


  • Upgrading your Authlogic Gem for Rails3

    If you’re using Authlogic in Rails3 (or if you’re upgrading your existing app from 2.3.8 to Rails3) you really need to be using the rails3 branch of the Authlogic gem. If you’re using Bundler, this is super-easy because you just need to update your Gemfile.

    In your Gemfile, update the authlogic line to be:

    gem 'authlogic', :git => 'git://github.com/odorcicd/authlogic.git', :branch => 'rails3'
    

    Now you should run $ bundle install to grab the new gem. Then, there are just a few more deprecated things in Rails3 that you’ll need to change in your upgraded app. I’ll try to enumerate here from memory, so please forgive me if I forget something.

    (more…)


  • InvalidAuthenticityToken on Logout when using Authlogic

    This one took me a while to debug. When upgrading my existing application to Rails 3 almost everything was working fine except whenever I would click the Logout link I would get an error because Rails couldn’t verify that the UserSessionsController#destroy request was valid from my app.

    This is the error message shown:
    ActionController::InvalidAuthenticityToken in User sessionsController#destroy

    actionpack (3.0.0) lib/action_controller/metal/ request_forgery_protection.rb:96:in `verify_authenticity_token'

    And the solution is simple, you just need to add the new csrf_meta_tag helper to your generated page (probably in the /views/layouts/application.html.erb file).

    (more…)


  • Running Rails 3 using RVM

    Getting RVM installed and managing two ruby environments (ruby 1.8.7 + rails 2.3.8 / ruby 1.9.2 + rails 3.0.0) went better than expected. This gist pointed me in the right direction. Now hopefully the process of migrating a rails 2.3.8 app to 3.0.0 will be as quick and easy.


  • A User Friend Relationship Model in Rails

    In building my first Ruby on Rails app, I needed to create a facebook / social-networking style friend relationship between users. The simple requirements were that it the friendship should require approval (e.g. a friend request followed by an accept or ignore) and it should be lightweight (not using two records for a single relationship).

    This method and code is inspired by two blog posts, which got me started but neither of which really fulfilled my complete requirement. The first used two records per friendship and the second was a twitter-style friend/follower without the approval process.

    So, here I break down my Friendship model which hopefully you will find useful and/or insightful as a complete solution or a starting point for your own implementation.

    (more…)


  • Rails Deployment: Engine Yard or Heroku?

    As I’m thinking about beta launching my first Rails app, I need to decide how to do the deploy.  Heroku and Engine Yard stand out as the most popular hosts, from what I see. Heroku also has a free option, which appeals to me.  Is one much better than the other?

    Long-term, I will probably host the app myself at my colo, but to keep things simple at first I don’t want to learn the intricacies of Rails hosting right now…


ruby on rails

This is the archive for ruby on rails.

FRESH / LATEST POSTS

FRESH / Twitter Updates

Error: Twitter did not respond. Please wait a few minutes and refresh this page.