Home arrow Ruby-on-Rails arrow Page 4 - Completing Tagging Support
RUBY-ON-RAILS

Completing Tagging Support


In this conclusion to a four-part series on adding tagging support to an ecommerce website with Ruby on Rails, you'll learn how to enable tag editing, and allow site visitors to view tags, among other things. 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 / 1
June 11, 2010
TABLE OF CONTENTS:
  1. · Completing Tagging Support
  2. · Modifying the Controller
  3. · Implementing the List Tags and Show Tag User Stories
  4. · Implementing the Recommend Books User Story

print this article
SEARCH DEVARTICLES

Completing Tagging Support - Implementing the Recommend Books User Story
(Page 4 of 4 )

Recall that the Recommend Books user story describes how George wants to be able to automatically recommend related books to customers. The acts_as_taggable gem has two methods that we can use for implementing this functionality:

  1. book.tagged_related: We can use this instance method to display books that share one or more of the same tags, and that are related to the current book being displayed to the customer.
  2. Book.find_related_tags: We can use this class method to recommend books that use tags that are related to the tags used by the currently displayed book.

Openapp/views/catalog/show.rhtmlin your editor and add the following code to it:

<dl>
  <dt>Price</dt>
  <dd>$<%= sprintf("%0.2f", @book.price) -%></dd>
  <dt>Page count</dt>
  <dd><%= @book.page_count -%></dd>
  
<dt>Tags</dt>
  <dd><%= display_tags @book -%></dd>
</dl>

<% if @book.tags.size > 0 %>
<div id="recommended"> <h2>Recommendations</h2>
<h4>Books</h4>
<% for book in @book.tagged_related %>
  <%= link_to book.title, :action => 'show', :id => book.id %><br/>
<% end %>

<h4>Tags</h4>
<% for tag in Book.find_related_tags(@book.tags.collect(&:name),➥
:separator => ',', :raw => true, :limit => 100) %>
 
<%= link_to tag['name'], :controller => 'tag', :action => 'show',➥
:id => tag['name'] %><br/>
<% end %>
</div>
<% end %>

We now show the tags on the page. We also added adivthat is used to recommend similar books and tags to the customer. Thisdivuses the idrecommended, so that we can style it with CSS. We want it to be displayed as a sidebar to the right of the page. We do this by adding the following topublic/stylesheets/style.css:

#recommended {
  border-left: 3px solid #666;
  background-color: white;
  position: fixed;
  bottom: 0;
  right: 0;
  width: 200px;
  height: 100%;
  padding: 5px 10px;
}

Note that the recommendations are done with theacts_as_taggableAPI.

Now add three new books with tags, as follows:

  1. For the first book, add the tagsRuby, Ruby on Rails, Programming
     
  2. For the second book, add the tagsRuby, Programming
     
  3. For the third book, add the tagsRuby, Ruby on Rails, Programming, E-Commerce.

You should see something similar to Figure 7-7 when accessing the second book. The system automatically recommends the first and third book, and theRuby on RailsandE-Commercetags to the customer.


Figure 7-7.  The book details page displaying recommendations in the sidebar

Related books are pulled out of the database by callingbook.tagged_related, which returns an array of books. Related tags are displayed with the code shown here:

<% for tag in Book.find_related_tags(@book.tags.collect(&:name),➥
:separator => ',', :raw => true) %>
 
<%= link_to tag['name'], :controller => 'tag', :action => 'show',➥
:id => tag['name'] %><br/>
<% end %>

We specify three parameters for theBook.find_related_tagsmethod:

  1. @book.tags.collect(&:name): This is shorthand notation for@book.tags.collect(|tag| tag.name), and returns an array of tag names. 
     
  2. :separator => ',': We specify that the separator is a comma. 
     
  3. :raw => true: We specify that we want an array of hashes returned, so that we can usetag['name']instead oftag[0], to access the tag names.

With five minutes left in the workday, we call in George to do some acceptance testing of the work done in this sprint. He’s happy with the results and tells us that he’s impressed at how incredibly fast we were able to finish the tagging functionality. He says, “That other consultant told me it would take a month to complete, and you do it in one day!

Summary

In this chapter, we showed you how to implement a tagging system that allows you to add tags to books and later edit them. Using the acts_as_taggable gem, we built a system that is able to recommend similar or related items to online shoppers. Along the way, we showed you how to use the console to test the model, write appropriate integration tests for the new functionality, and implement an autocompletion field using the script.aculo.usJavaScript library.  

In the next chapter, we’ll secure our application.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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