Get updates

Development

Scott Introduction to Regular Expressions in JavaScript

If you’re a programmer who has ever had to develop an application dealing with text input (which is most of them), from files, databases, or user input, you might be interested to know that regular expressions can make your job a lot easier.

Unlike most dry regular expressions tutorials out there (Mike’s smart post aside), I intend to provide more than just the “what”; I’ll walk you through the “how” and “why” too. After all, why would you care to learn regular expressions if you don’t get to find out about some of the cool and powerful things you can do with them?

Continue Reading…

Scott Q&A: What Methods are Available with acts_as_list?

When you make use of Active Record’s convenient acts_as_list modifier in Rails, what does this do for you? Most of the info out there describes the way acts_as_list manages position of items, ordering them like items in a, well, ordered list.

That’s great, but what about new helper methods provided by acts_as_list? Doesn’t it make juggling list items easier? Or do you simply have to fend for yourself when you want to rearrange your list?

Question: What additional methods are provided when using acts_as_list?

Continue Reading…

Scott Chomp for JavaScript

In most programming languages, there exist certain simple and useful functions that people come to rely upon. From my days with Perl, chomp was one of those functions for me.

Given a string, chomp bit off all newline characters at the end of the string (and returned the number of newline characters it removed).

Continue Reading…

Scott Easing into Rails Deployment with Mongrel

Like the good [Rails] book says, Apache plus FastCGI is the way to go. Or is it?

It’s been a while since I’ve been free to work on ListFinity, just recently I was able to start addressing the question of deployment: How can I get my Rails application running on a local server, in “stable” production mode while I tinker away separately on the development version?

Of course I had to start by following my Rails book’s advice.

The much-touted solution of Apache plus FastCGI required, well, getting Apache running. Okay, done. Then CGI- check. Then Rails with CGI on Apache… wow we’re losing altitude quickly.

Continue Reading…

Scott JavaScript Check All Checkboxes

Have you ever used a site with a bunch of checkboxes, and you needed to check all of the checkboxes but they didn’t provide that feature directly in their user interface? You might shrug, and click away until the job is done… or use the “JavaScript command line” aka your browser’s address bar, to do it the power-user way.

I realize that may not be a common problem, but in my day job I kept running into it while adding hundreds of keywords at MSN’s adCenter (wanting all of them to use all match types).

Hasn’t someone else already solved this problem? Seems very likely, but my searches were only pulling up examples of “check all” functions to use on your own site, so after a bit of coding I present to you my check-all checkboxes function:

Continue Reading…

Scott Q&A: How to use plain JavaScript in RJS templates

What’s all the fuss over RJS templates for Rails? JavaScript is not, in my opinion, the dirty/scary/tedious or otherwise lame language some people seem to think it is. I actually enjoy writing JavaScript objects, and currently the codebase of my hierarchical list manager app contains about as much JavaScript as Ruby code.

Despite my JavaScript predilection (or because of it), I am happy to work with RJS templates. Why? Because they make working with JavaScript (and client-side capabilities JavaScript offers) in Rails a whole lot easier. A lot of what makes Rails really fun is stuff like the easy integration of Ajax and JavaScript for the UI, such as Scriptaculous.

RJS templates make it a snap to respond to an Ajax request with much more than just an update for a single element, through a number of wrapper functions that generate JavaScript for you. Yet sometimes you’ll still want to interact with your own custom JavaScript functions and objects.

Question: How can plain JavaScript be used inside RJS templates?

Continue Reading…

Scott Q&A: How to get WordPress home to link to a single previous post?

WordPress is a handy blogging platform, especially when it’s time for customization. By default the home page shows a number of full posts and then links to another page with multiple full posts. For some this works fine, but I’d prefer not to contend with duplicate content issues and instead let older posts speak for themselves on their own unique pages.

Question: How can the home of a WordPress blog be made to forgo links to pages full of multiple previous posts, and just use a link to the single previous post instead?

Continue Reading…

Scott Q&A: How to include a Rails text field without a variable?

The form helpers in Rails are great. But what do you do when you want to collect some data that’s not tied directly to a variable in your model?

Continue Reading…

Scott Passing :id When Rendering a Component

For the time being I’ve decided to forgo acts_as_tree and roll my own solution for nested list display. At this point I don’t think I really need to parse and display the entire tree in one fell swoop, it seems more natural to display the top level and retrieve children of a node when the user clicks and expands the node.

Since my “list” action has been doing fine to display list items, I quickly made it context-aware and used it as a component to render the children of a node when an item is expanded. Consulting my rails book I tried to render like this:

Continue Reading…

Scott Displaying a hierarchy and acts_as_tree

What’s brewing over at ListFinity, home of infinite lists?

Well, supposedly right now I’m working out various means of constructing and manipulating “infinitely nestable” lists, as in an object hierarchy (aka tree view) of arbitrary depth.

That’s all well and good, but not as straightforward to code as the display of a single unordered list. I figured that such a hierarchy would be a good candidate for recursion, and in this case that didn’t sound like too big a chore to write… except when I started to imagine that each loop back to the recursive method would want to pass on the list data for the next loop to walk through another level, etc. This triggered a flash of nightmares where a sufficiently large or deep list (nevermind one mangled to include circular references) spurs the recursion on to eat memory like a mosquito with its proboscis stuck in the jugular.

Continue Reading…

Scott Q&A: How to Set a Form’s ID in Rails?

HTML is not hard. Anyone who can program, in any language, should be able to write HTML with little or no training. Yet there are reasons why the HTML helper functions in Rails can be handy. Chief among them is the easy and fairly clean way you can incorporate variables and other constructs; after all, the idea here is to have an app generate some of the code rather than having to write every piece by hand.

I was happily marching to the tune of form_tag() when I decided that I wanted a little JavaScript function to interact with the form’s contents. Great, except the HTML form code being generated by that helper function wasn’t coming with its own id attribute.

I could probably have the JavaScript function refer to this particular form by indexing an array, but then it could break if I ever changed the layout of the page to include another form. Hardly ideal.

Question: How does one tell Rails what the form’s id should be when creating the form with the helper function (or the Ajax form_remote_tag)?

Continue Reading…

Scott Scriptaculous Effect.Squish afterFinish callback fix

Previously as I was using Scriptaculous I ran into a problem with afterFinish on Effect.Squish. This is my solution.

Continue Reading…

Scott The Scriptaculous afterFinish problem

On the heels of working out the proper syntax to use when returning JavaScript to execute after an Ajax request, I ran into trouble with a Scriptaculous effect callback.

Continue Reading…

Scott Q&A: How Rails Returns JavaScript to Execute on Ajax Request?

On the road to a better to-do list application, I need to start with a solid base and advance one step at a time. That base includes simple ajax functionality so list items can be manipulated without a full page reload.

Question: When the user clicks an Ajax link to destroy a list item, how do I have Rails return JavaScript code to be executed which will hide that item?

Continue Reading…

Scott ListFinity: The Gathering

…really it’s not so much a gathering, just me typing away and maybe the occasional contribution from my cat… ach, great, that’s really going to draw in the reader…

Thus begins my first real Ruby on Rails project.

Continue Reading…

3DM Design is where Scott Drake publishes thoughts on programming web applications in JavaScript and Ruby.

Fatal error: Uncaught Error: Call to undefined function ereg_replace() in /home/dmetro/3dmdesign.com/wp-content/plugins/popularity-contest/popularity-contest.php:2531 Stack trace: #0 /home/dmetro/3dmdesign.com/wp-content/plugins/popularity-contest/popularity-contest.php(2495): akpc_is_searcher() #1 /home/dmetro/3dmdesign.com/wp-includes/class-wp-hook.php(298): akpc_api_footer_javascript('') #2 /home/dmetro/3dmdesign.com/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters(NULL, Array) #3 /home/dmetro/3dmdesign.com/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #4 /home/dmetro/3dmdesign.com/wp-includes/general-template.php(2605): do_action('wp_footer') #5 /home/dmetro/3dmdesign.com/wp-content/themes/hex_columns/footer.php(36): wp_footer() #6 /home/dmetro/3dmdesign.com/wp-includes/template.php(688): require_once('/home/dmetro/3d...') #7 /home/dmetro/3dmdesign.com/wp-includes/template.php(647): load_template('/home/dmetro/3d...', true) #8 /home/dmetro/3dmdesign.com/wp-includes/general-template.php(84): locate_t in /home/dmetro/3dmdesign.com/wp-content/plugins/popularity-contest/popularity-contest.php on line 2531