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).
George must be able to go back to a book that he has added to the system and edit the details, including removing and adding tags. The edit book page uses the same_form.rhtml partial as we used for the add book page. This means that most of the work has already been done in the previous section. The only part of the code we need to change is the controller. As usual, we will start by creating the integration test.
Updating the Integration Test
Change thetest_book_administrationmethod in the integration testtest/integration/ book_test.rb. Add thetagsparameters to the line that starts withgeorge.edit_book, as shown here:
george.edit_book(ruby_for_dummies, :tags => 'Toddlers', :book => { :title => 'Ruby for Toddlers', :publisher_id => publisher.id, :author_ids => [author.id], :published_at => Time.now, :isbn => '123-123-123-X', :blurb => 'The best book released since "Eating for Toddlers"', :page_count => 123, :price => 40.4 })
Also change the implementation ofedit_bookDSL method as follows:
def edit_book(book, parameters) get "/admin/book/edit/#{book.id}" assert_response :success assert_template "admin/book/edit"
post "/admin/book/update/#{book.id}", parameters assert_response :redirect follow_redirect! assert_response :success assert_template "admin/book/show"
book.reload assert_equal parameters[:tags].split(',').size, book.tags.size end
Note that we reload thebookobject from the database, before we check that the tags have been updated.
Run the test, and you should see it fail with the following error message:
-------------------------------------------- <1> expected but was <3>. --------------------------------------------
You get the error because we haven’t yet modified the controller.