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).
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:
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.
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:
<% 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:
Note that the recommendations are done with theacts_as_taggableAPI.
Now add three new books with tags, as follows:
For the first book, add the tagsRuby, Ruby on Rails, Programming.
For the second book, add the tagsRuby, Programming.
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:
@book.tags.collect(&:name): This is shorthand notation email@example.com(|tag| tag.name), and returns an array of tag names.
:separator => ',': We specify that the separator is a comma.
:raw => true: We specify that we want an array of hashes returned, so that we can usetag['name']instead oftag, 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!
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.