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

You can now try to access the page with your browser. It should generate the following error, as the view expects to find the tags instance variable:

--------------------------------------------
You have a nil object when you didn't expect it!
--------------------------------------------

Fix this by changing theloadmethod in the book controller (app/controllers/admin/ book_controller.rb), as follows:

  def load_data
    @authors = Author.find(:all)
    @publishers = Publisher.find(:all)

    @tags = Tag.find(:all)
 
end

Recall that we use theload_datamethod to load all authors, publishers, and tags in thenewandeditactions. Now, if you openhttp://localhost:3000/admin/book/new, you should see the new field.

Add a book and type in a few tags, such asProgramming, Rails, Ruby. Save the book, and create another book. Type the character R  in the Tags field. This time, you should see a list of tags containing both Rails and Ruby.


Note  As stated earlier, this example does not use Ajax. Instead, the tags are all stored in a JavaScript variable. Using Ajax would be easier. The autocompletion text field could be generated with the following code:<%= text_field_with_auto_complete :book, :tags %>This would also require us to add theauto_complete_for_book_tagsaction to the controller, which would render a partial view.



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