Home arrow Ruby-on-Rails arrow Page 2 - Implementing Tagging Support
RUBY-ON-RAILS

Implementing Tagging Support


In this third part to a four-part article series on implementing tagging support for an ecommerce website using Ruby-on-Rails, you'll learn how to set up the application so users can assign tags to items. This article is excerpted from chapter seven of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
June 10, 2010
TABLE OF CONTENTS:
  1. · Implementing Tagging Support
  2. · Modifying the View
  3. · Modifying the Controller
  4. · Changing the Style Sheet

print this article
SEARCH DEVARTICLES

Implementing Tagging Support - Modifying the View
(Page 2 of 4 )

Next, we need to change the view. As we explained earlier, the view should have an autocompletion field where George can enter the tags. This functionality could easily be implemented with Ajax, but instead, we’ll do it the old-school way with plain JavaScript—just to save some resources and a couple of Ajax calls to the server.

The tags will be stored in a JavaScript variable and passed to theAutocompleter.Localhelper provided byscript.aculo.us.


Tip  The documentation forAutocompleter.Localcan be found on thescript.aculo.uswiki athttp://wiki.script.aculo.us/scriptaculous/ show/Autocompleter.Local.


Next, add the new Tags field to the view fileapp/views/admin/book/_form.rhtml, right after the Title field:

<p>
<label for="tags">Tags</label>
<input type="text" id="tags" autocomplete="off" size="50" name="tags" value="➥
<%= @book.tags.collect{|tag| tag.name }.join(",") if @book.tags -%>">
</p>
<div id="tags_update"></div>
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
  new Autocompleter.Local('tags','tags_update',
 
new Array(<%= @tags.collect{|tag| """ + tag.name + """ }.join(",") %>),➥{ tokens: new Array(',','\n'), fullSearch: true, partialSearch: true });
// ]]>
</script>

Notice that we specify the tag separator to be a comma with thetokensparameter. This is an array and can contain more than one separator. For example, it could also contain the new-line character\n. Also notice that the tags will be shown in thetags_update div.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials