Home arrow Ruby-on-Rails arrow 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
(Page 1 of 4 )

Implementing the User Stories

Web applications like Flickr and del.icio.us allow their users to tag content by entering a list of tags, separated by spaces, in a text field; for example, they may enter News Europe London. This is a simple and elegant way of tagging, and is easy enough for George to understand and use. To make George’s life even easier, we’ll use autocompletion to help him remember the tags that have previously been assigned to books. When George types in, for example, the string Beerin the Tags field, the system should display all tags containing that string, includingBeer TastingandBrewing Beer. This will prevent George from entering duplicate tags.  

Implementing the Assign Tags User Story

According to our requirements, George should be able to assign tags to a book on the add book page. We’ll modify the page and add an autocompletion field that uses the script.aculo.us JavaScript library, which we introduced in Chapter 5.

Updating the Integration Test

First, we’ll modify the integration test and add a test that simulates George entering the tagsElvis,Thriller, andCookingin the Tags field. Change thetest_book_administrationmethod intest/integration/book_test.rbas follows (changes are marked in bold):

  george = new_session_as(:george)
  george.add_book :tags => 'Ruby, Programming, Dummies', :book => {
   
:title => 'Ruby for Dummies',
   
:publisher_id => publisher.id,
   
:author_ids => [author.id],
   
:published_at => Time.now,
   
:cover_image => fixture_file_upload('/books.yml', 'image/png'),
   
:isbn => '123-123-123-X',
   
:blurb => 'The best book released since "Eating for Dummies"',
   
:page_count => 123,
   
:price => 40.4
  }

The change adds thetagsparameter, and the specified tags, to the request. This parameter will be used by the controller to tag the book. Note that we can’t put it in thebookhash, as that would make Rails try to assign a string to thetagsattribute, which it expects to be an array ofTagobjects.

We want the test to fail in TDD fashion, but if you try to run it now, it will succeed. This is because we haven’t yet added a check that verifies that the book was tagged.

Update theadd_bookmethod as follows (changes are marked in bold):

  assert_response :success
  assert_template "admin/book/list"
  assert_tag :tag => 'td', :content => parameters[:book][:title]
 
book = Book.find_by_title(parameters[:book][:title])
  assert_equal parameters[:tags].split(',').size, book.tags.size
  return book
end

With the change, we check that the book can be found by its title and that it is tagged. If you run the test now, it will fail and print out the following message:

--------------------------------------------
<3> expected but was
<0>.
--------------------------------------------

We haven’t created the code that actually tags the book, so the test fails when it checks the size of thetagscollection. This is easily fixed by changing thecreateaction in the controller, as follows:

  def create
    @book = Book.new(params[:book])

    @book.tag(params[:tags], :separator => ',')

    if @book.save
     
flash[:notice] = 'Book was successfully created.'
     
redirect_to :action => 'list'
    else

Thecreate action calls thebook object’stagmethod to assign the tags. Recall that the tags are specified with thetagsparameter, and that the comma is used as a separator. Note that if the user doesn’t enter any tags, this will do nothing.

Run the integration test again with the following command:

$ ruby test/integration/book_test.rb

This time, it should pass without errors.


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-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials