Get updates

Q&A

Scott Q&A: How to Customize Rails to_json output?

Rails has a bunch of JavaScript helpers, but once you move beyond those and want to write your own JavaScript code for your application, you may find yourself dealing with some JSON produced on the server-side.

By default, the to_json method in Rails is probably a lot more verbose than most people want. I’ll share a few tips on how to tame it.

Question: How can I slim down the output of to_json in Rails?

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 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 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…

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