Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

20110326

jQuery autoSuggest vs rails (and acts_as_taggable_on)

While AutoSuggest is quite wonderful as it is, there's also a lot of room for improvement (e.g. I've started using this fork since the original author doesn't seem too community-friendly).

Here's one tip on how to use it with rails (and simple_form) — or rather how to workaround the following issue.
When you write something like $("#post_tag_list").autoSuggest(...); for the first time, you'll expect AS to do all the wow stuff on the client side and have the original input field with the values as a parameter back on the server side, right?
Well, I did.
Unfortunately, you have to work harder: not only you must include the asHtmlID: "tag_list" option in the autoSuggest parameters, but (since that option actually defines the id's suffix only) you'll have to patch your controller allong the lines of:
before_filter :autosuggest_fix
and
def autosuggest_fix
params[:post][:tag_list] = params[:as_values_tag_list] if params[:post]
end

a jQuery templates tip: checking for optional fields

One tip regarding the templates

“Comments for this page are closed.” for some reason, so here:
When rendering an optional numerical field, the safest way to check on its existence is:
{{if typeof $item.data.optional_field == "number"}}...{{/if}}
(for other types, change the right side of the == accordingly)

The main problem with the way described in the doc is possible accidental name collision of the field's name with some other variable in the scope.