Monday, June 25, 2007

submit_to_remote

I made a novice mistake today - tried to nest one form inside another. The functionality I was looking for is to create an entity that involves pictures. On the page where the entity gets created, I wanted to prefetch the picture specified in a text field via ajax. My initial solution was to make a standard form using rails form_for and link_to_remote. But for the life of me I could not get the link_to_remote to put the value of the text field into params. I don't think it's possible. So I just wrote my own link using prototype's Ajax.updater. The called action rendered an RJS template that made the image tag to display the picture.

But I got the weirdest result: the javascript was being rendered into the browser, instead of the result of the javascript. Thank goodness Brian put his finger on it: wrong mime type being supplied in the response. Why did that happen? Because I mixed my own ajax scripting with the rails scripting in RJS. So I converted everything to my own ajax calls. Worked fine but I'd be stuck with maintaining my own javascript for the rest of the application. Yuk.

So I backed out the ajax script and re-thought the application. My next attempt was to insert a remote_form_for. Which of course died because it creates a form nested inside my main form. HTML does not permit nested forms. Turns out rails has a helper exactly for this: submit_to_remote. It not only makes the ajax call without creating a form, it also puts each of the form fields into params for you. Problem solved.

Saturday, June 16, 2007

Quotes

Note to self. I love this style for blockquotes and must find a use for it in the journal feature of the travel portal. It's a Wordpress theme used by Noah Winecoff.

Friday, June 8, 2007

More than development.log but less than breakpointing

Ran into a problem today that wouldn't reveal itself in the development.log but didn't seem worthy of firing up breakpointing. I had added some more parsing features to the tour operator site scraping tool and I got odd errors on a few pages. The errors didn't show up right away, they were toward the end of the page scraping process. Quick background: The pages consist of tour itineraries listing the activities of each day throughout the tour. Fortunately each day has the same markup. Using hpricot the tool first collects the contents of tags that identify the day of the tour, then cycles back to collect the city each day is at, then cycles back to get the description of each day's activities, and so on. Many passes through the same datastream. At the end, the tool splices all the collections together to form records corresponding to each day's data points.

The error was appearing toward the end of the process. Breakpointing would have had to jump in and out of irb many times before the error appeared.

I had forgotten about logger - the ruby facility to generate your own log messages. So easy to use:
logger.info("processing day: #{itin_day.inner_html[4,3]}")

writes "processing day: 4" to development.log. Half a dozen of these scattered through the tool provided enough information in the log to see exactly where the problem was.

Thanks logger!