Home arrow Ruby-on-Rails arrow Page 3 - 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 List Tags and Show Tag User Stories
(Page 3 of 4 )

The Show Tag user story describes how customers should be able to view all tags in the system. Before we continue with the next user story implementation, George reminds us that he expects us to complete the three remaining user stories today: I have friends in Bangalore! Ill just offshore the whole project to India, if you cant get it done today.

To speed up development, well use thegeneratescript to create a new controller. Execute thegenerate script with the following parameters to generate the controller for these user stories:

$ script/generate controller tag list show

exists app/controllers/
exists app/helpers/
create app/views/tag
exists test/functional/
create app/controllers/tag_controller.rb
create test/functional/tag_controller_test.rb
create app/helpers/tag_helper.rb
create app/views/tag/list.rhtml
create app/views/tag/show.rhtml

 

 

 

 

 

 

 

This creates the controller, functional test, helper, and views. The List tags and Show tag user stories are easy to implement, as well show you next

First, open thetag_controller.rbfile, and change thelistandshow actions as shown here:

  def list
  
@page_title = 'Listing tags'
  
@tag_pages, @tags = paginate :tags, :order => :name, :per_page => 10
  end

  def show
   
tag = params[:id]
   
@page_title = "Books tagged with '#{tag}'"
   
@books = Book.find_tagged_with(:any => tag, :separator => ',')
  end

Thelistaction uses the standardpaginatehelper to show a paginated list of tags. Theshowaction uses thefind_tagged_withmethod to find books having the specified tag.

Next, add the following code toapp/views/admin/books/list.rhtml:

<% for tag in @tags %>
  <%= link_to tag.name, :action => 'show', :id => tag.name %><br/>
<% end %>
<p>
<%= link_to 'Previous page', { :page => @tag_pages.current.previous }➥
if @tag_pages.current.previous %>
<%= link_to 'Next page', { :page => @tag_pages.current.next }➥
if @tag_pages.current.next %>
</p>

The view loops through the tags and generates a link to the show tag page. At the bottom of the page we show the pagination links.

For the show tag page, we need to add the following toshow.rhtml:

<% for book in @books %>
 
<%= link_to book.title, :controller => 'catalog', :action => 'show',➥
:id => book %><br/>
<% end %>

This simply loops through all the books and links to the book details page we implemented in Chapter 4. Now you can do a quick test by accessinghttp://localhost:3000/tag/list. You should see a list of tags, as shown in Figure 7-5.


Figure 7-5.  The tag list page showing a list of tags

If you click a tag, you should see all books that have been tagged with that specific tag, as shown in Figure 7-6. Clicking the book title takes you to the book details page.


Figure 7-6.  The show tag page

We still dont have a link to either the tag list or the show tag page. It would be natural to link to the show tag page from all the places where the tag is being displayed. Luckily, we used a helper to display the list of tags that have been assigned to a book, so the change is just in one place. Change thedisplay_tagsmethod inapp/helpers/application_helper.rbas follows:

  def display_tags(book)
   
book.tags.collect{|tag| link_to tag.name, :controller => '/tag',➥
:action => 'show', :id => tag.name }.join(", ") if book.tags
 
end

Instead of just showing the tags name, we now link to the show tag page.


Note  Notice that we needed to prepend a forward slash to the controller, so that the admin pages also point to the correct page. If we had used only:controller => 'tag', the generated URL would be/admin/tag/show/x.


Now, if you access any of the pages where we display a book and the tags, the tags should be clickable and point to the show tag page.

A good place for putting a link to the tag list page would be the menu. To do this, add the following code toapplication.rhtml:

  <li><a href="/admin/book">Books</a>&nbsp;|&nbsp;</li>
 
<li><a href="/tag/list">Tags</a>&nbsp;|&nbsp;</li>
 
<li><a href="/">Catalog</a>&nbsp;|&nbsp;</li>
 
<li><a href="/about">About</a>&nbsp;</li>


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