Home arrow Ruby-on-Rails arrow Page 2 - 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 - Modifying the Controller
(Page 2 of 4 )

Next, change theeditaction inapp/controllers/admin/book_controller.rbas follows:

  def update
    @book = Book.find(params[:id])
   
@book.tag(params[:tags], :separator => ',', :clear => true)

    if @book.update_attributes(params[:book])
      flash[:notice] = 'Book was successfully updated.'

By setting the:clearparameter totrue, we first empty the collection of tags, before assigning the new one.

Run the integration tests withruby test/integration/book_test.rb. This time, all tests should pass without errors.

Modifying the Views

We have finished the implementation of this user story. Now, we need to check that it works by editing an existing book that has been assigned some tags. Add or remove a tag and verify the results. Notice that thereís one small problem we should fix before moving on to the next user story: the book details and book list pages should display the tags to the administrator.

Start by changing the book list view. Openapp/views/admin/book/list.rhtmlin your editor and modify the existing code as shown here:

  <tr>
    <th><a href="?sort_by=publisher_id">Publisher</a></th>
    <th><a href="?sort_by=title">Title</a></th>
   
<th>Tags</th>
   
<th><a href="?sort_by=isbn">ISBN</a></th>
    <th colspan="3"></th>
  </tr>

<% for book in @books %>
 
<tr>
    <td><%= book.publisher.name %></td>
    <td><%= link_to book.title, :action => 'show', :id => book %></td>
    
<td><%= display_tags book %></td>
   
<td><%= book.isbn %></td>
    <td><%= link_to 'Show', :action => 'show', :id => book %></td>

Notice that we have introduced a new helper method calleddisplay_tags, because we need to display the tags on both pages. Instead of duplicating the code in the view, we add the following method to the application helper (app/helpers/application_helper.rb):

  def display_tags(book)
    book.tags.collect{|tag| tag.name }.join(", ") if book.tags
  end

By adding the new method to the application helper, we can now access it from any view in our application.

After making the change, accesshttp://localhost:3000/admin/book/list. You should now see the tags displayed in the list, as shown in Figure 7-3.


Figure 7-3.  The book list page showing the Tags column

Next, change the book details view file. Openapp/views/admin/book/show.rhtmland add the following after the Title field:

  <dt>Tags</dt>
  <dd><%= display_tags @book %></dd>

Now, when you access the book details page, you should see the tags on the screen, as shown in Figure 7-4.


Figure 7-4.  The book details page showing the book's tags

We are now confident that the administratorís part of the tagging functionality works as George wants. We tell him to come over and have a look. Again, he seems happy that it is so simple and that it works like a train on rails.


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