Thursday, December 23, 2010

Capybara labels not working

Actually, they are.

Say you have a post model, and you gave it the label foobar.
Now you try to use that label to refer to the model you've created/found.
Consider the following example:



This example, rather than failing by saying the model did not match the fields passed into it, violently fails by giving the error:


Not very pretty, right?
In fact, I personally mistook this for having something wrong with my labels.
Turns out, the label is totally fine, but the error thrown, well... threw me off...

It took me way too much time to go through any bit of capybara documentation I could lay my hands on, and I still not understand why I got the error.

Finally, turns out Pickle raises this error, not because this label is not valid, but rather because the model "myteacher" doesn't match the attributes I'm requesting.

For example: the following test, which uses the same label is actually passing.


Hopefully this saves you some good quality time between your head and your favorite wall.
I emailed the developer of Pickle and I'm hoping he'll be kind enough to change the error a bit in future versions so that it is clearer what's happening.

Capybara and Selenium in Rails 3 tests fail and browser doesn't start

I had an issue with Firefox not starting to run Selenium in Rails 3.
Instead, my tests failed, giving me all red (even for steps that were previously passing without JS). Turns out this is caused by a dependency issue.
Someone thankfully posted a question on StackOverflow that helped me get through this.
To fix it, simply run add this to your Gemfile

Then run

This should fix it for you.

You can also track this issue at Github, where I posted this very solution.

Friday, November 26, 2010

How to Apply App Templates to Existing Rails Applications

If you haven't yet discovered the power of rails app templates, then chances are you're wasting valuable time setting up applications in the same way over and over again. I encourage you check out Ryan Bates' railscast on them.

This post, however, is not about application templates (you can find plenty of info online already), but about how to apply them to existing templates.

In my Rails app, I created a file called automate.rb. To apply it to my app, I enter the following nifty rake command into the command console:



This runs the contents of automate.rb on the existing rails application. Perfect.

Monday, August 30, 2010

Authlogic browser stuck (hang) on login (authlogic_facebook_connect)

The Issue

I am integrating facebook connect with my application which uses authlogic, facebooker, and the authlogic_facebook_connect plugin. After I log in with facebook, I get redirected to the front page of the site (as per my code) - but the page never loads! - it hangs. Looking at the development log, I see that something is continuously trying to load a user from the database.

The Explanation

This isn't specifically an authlogic_facebook_connect issue, but rather an issue in authlogic - it should just create a persistence_token in the database if it doesn't find one - not continuously try to find the user with a non-existent persistence_token.
Since I'm only using facebook connect for my site, though, the initial persistence_token creation that happens when a regular registration happens doesn't occur.

The Fix

1. First of all, let's make sure that the database field cannot be set to null so we get a database error if it is null at any point. Make sure that your persistence token's null parameter is set to false when you create the users table, as such:


2. Let's make the authlogic_facebook_connect plugin force a persistence_token reset when a user gets created.
Inside vendor/plugins/authlogic_facebook_connect/lib/authlogic_facebook_connect/session.rb, locate the validate_by_facebook_connect method.
Locate the lines:



And add another line underneath them to get the final result:



Now a persistence_token gets created when a user gets registered, and it's never NULL from that point on.


3. Finally, modify your existing database users' authenticity_token to anything other than NULL (i.e. RoRIsFun), so next time they log in, the browser hang isn't going to happen. I personally had nothing in my database, so I just dropped and migrated again, as such:



Update: Just found out that specifically with authlogic_facebook_connect, the regular flow leads to an inaccurate login_count (users are initially logged in twice). To solve this problem (and the previous problem), simply change

into


This would make sure you're only logged in once to the site, and that the user model gets validated. This also cause the automatic invocation of reset_authenticity_token by the normal validation flow of authlogic. This was originally disabled by authlogic_facebook_connect for reasons that are beyond me.

Sunday, August 29, 2010

Facebook Connect: xd_receiver Doesn't Close on Cancel

I'm currently working on integrating a rails app with Facebook Connect.
One of the annoying things I found out during the integration was the lack of action if a user decides to deny access to the site: the facebook login popup simply links to a blank /xd_receiver.htm with various query string parameters. Mine looked like this:



Notice the presence of fbCancelLogin in the query string? We can use that to our advantage.
Add the following script tag at the top of xd_receiver.htm: