Broken Flickr Sidebar

Posted by JP Sat, 19 May 2007 20:36:00 GMT

I finally decided to subscribe to Flickr for a number of reasons, one of which is that I wanted to order some Moo MiniCards.

I've been wanting to get some business cards done for almost who years now, but never settled on a design. When Simon Law handed me one of his Moo MiniCards at the Montreal DemoCamp2, I was convinced that that was the way to go.

The Moo cards might not be suitable for the corporate world, which is fine since I'm trying to steer clear from the I-wear-a-suit-and-tie population in general. On a plus side, if I get a weird look when I give one out to someone, I know I do not want to do business with that person. If someone judges you by the size of your business card, you definitely don't want to deal with him. Anyways, I don't.

End rant.

Having a Flickr account, I went on to add a feed of my pictures to my blog's sidebar. Turns out that the version of Typo which I used (Site5's generic blog installer) isn't compatible with the Flickr feeds' current format. I'll try to remedy this over the weekend if I can get to squeeze some "me" time out if it.

Posted in  | Tags , , ,  | 3 comments

Tip of the Day: Creating 'Back' Links in Rails

Posted by JP Thu, 01 Mar 2007 21:54:00 GMT

One of the things you need to do frequently in web applications is provide a link for the user to navigate back one page when the user enters a form.

For our example, let's consider you're building yet another blogging application and providing a page which lets the user add a new Post to the blog. From that page, you want to provide a link to the previous page, in case the user changes his mind.

The quick-and-dirty way of doing this would be linking to the list action, since the will usually come from a page listing all the Posts.

<%= link_to 'Back', :action => 'list' %>

This will work.

Oh, wait... maybe not. What if the previous page had an extra parameter, let's say to list all Posts belonging to a specific Category.

We can fix this:

<%= link_to 'Back', 
            :action => 'list',
            :category_id => @category_id %>

Ok, this will work, assuming that the link to the new action passed on the category_id parameter to the new action, which would be logical.

That's better.

But what if list had another parameter that wasn't passed on the the new action, like a sort_order parameter?

Things can easily get complicated and mistakes can easily be made, leading to unelegant broken links.

The proper way to do this is as follows:

<%= link_to 'Back', request.env['HTTP_REFERER'] %>

This will work in all situations, even when the previous list action was the result of a create or update, since these actions normally redirect to the list action. The 'Back' link will effectively link to that redirection, and not pop the unelegant The page you are trying to view contains POSTDATA message.

One last thing to consider. What if the new page is the first page the user visits after launching the browser (e.g.: followed a link in an email). Where will 'Back' link to? The browser will link to the current page! How useless. Let's reiterate our solution to avoid this irritation. There are two acceptable solutions:

<%= link_to_unless request.env['HTTP_REFERER'].nil?,
                   'Back',
                   request.env['HTTP_REFERER'] %>

will always display 'Back', but it won't be clickable if there is no referrer.

<%= link_to 'Back',
            request.env['HTTP_REFERER'] 
    unless request.env['HTTP_REFERER'].nil?

will not display 'Back' unless there is a referrer.

Both these solutions are good enough. Why good enough? If by clicking 'Back' you go back to a page that had a 'Back' link - for example, when you submit a form with invalid data, you only get one properly functioning link. It's a small caveat since once the user submitted a form with invalid data, it's unlikely that he'll want to go back and not edit at all. There's always the main menu and in last resort, the browser's own 'Back' button.

And for those users familiar with the Rails Recipe book, using link_to_unless_current conjointly with the Cleaning Up Controllers with Postback Actions recipe will not prevent the links from being displayed. I tried.

One question remains: why is HTTP_REFERER missing an 'R'? One would think mistakes like this wouldn't get through.

Posted in  | Tags , ,  | 3 comments