Old Guy New Trick

An old guys journey to learn how to code.

Hitting the Links...

Author: John on January 13, 2016

Recently I was tasked to fix the printing on one of our views where the bidder can print the results for a round of bidding.  When the user went to the results view and clicked on the printer icon, the output was as expected.  But if they choose a previous round from an available round drop-down, they were not getting the appropriate data.

Let's take a look at the code in our view that was in place for the print icon:

= link_to image_tag("icons/printer.png", border: 0), {controller: "results_summary/print", action: print}, class: "print_button noprint", id: "printer", :target => "_blank"

Standard Rails code for an image tag and link.  If this looks a little funny to you, we use HAML for the creation of our view files.  When you hover over the print icon of this page the url in the status bar would read:  localhost:3000/results_summary/print

The problem comes into play when the user wants to print the results from a previous round.  When they, for example, choose Round 2 from the drop-down, and hovered/clicked on the printer icon, the url was still localhost:3000/results_summary/print.  In order to get the correct data, we needed to pass an argument/parameter that specified the round.  In short, we need the url for Round 2 to look like:  localhost:3000/results_summary/print?round=2

So I thought I would be clever and make a small change to our existing view model so I could determine which round to use and then just modify the url.  I made these two chanages:

#update to the view model:
def print_round
  "?round=#{round.id}" unless round == current_round

#update to the link_to in the view file:
= link_to image_tag("icons/printer.png", border: 0), {controller: "results_summary/print#{@summary.print_round}", action: print }, class: "print_button noprint", id: "printer", :target => "_blank"

However, when I used the above code, and hovered over the print icon, I saw the following:

Well, that isn't what I wanted.  Why the heck am I seeing the %3F where I want a question mark?  Must be some kind of html escaping being done.  As I looked at my proposed fix, and the %3F staring at me, my spidey sense was tingling, trying to tell me that I had seen this before.  And indeed I had.  However I couldn't remember how I had resolved this before.

Having a pair/mentor sitting beside you can really come in handy.  I think he saw the blank look on my face, so he asked what was up.  I showed him what I was trying to do.  Apparently he has seen this before as well.  I saw the Rails API docs fly across his screen and then he said for me to update my solution to reflect the following:

#proper update to the view model:
def print_round
  round.id unless round == current_round

#proper update to the link_to in the view file:
= link_to image_tag("icons/printer.png", border: 0), {controller: "results_summary/print", action: print, round: @summary.print_round}, class: "print_button noprint", id: "printer", :target => "_blank"

Works well when you use the correct code.  Since this was the second time I had to solve a very similar problem, and I couldn't find my previous solution, I thought it would be great to write a blog post about it.  If you happen to find yourself in a similar situation, and use the solution above, or a different solution, please let me know.

Learn Something New Everyday

Last Edited by: John on January 13, 2016