Completing a Book Inventory Management System
(Page 1 of 2 )
In this conclusion to a 12-part article series on building a book inventory management system with the scaffolding feature of Ruby on Rails, we'll finish the View Book User and Edit Book User stories. This article is excerpted from chapter three of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Completing the View Book User Story
The View Book user story also needs some cleaning up before George is happy. The code created by the scaffolding displays the values of all database columns directly to the user. This means, for example, that the publisher’s ID is shown instead of the publisher’s name. We’ll fix this and also add code that displays the authors of the book and the book cover.
Changing the View
First, changeapp/views/admin/book/show.rhtmlas follows:
<p><%= link_to "Edit", :action => "edit", :id => @book %> |
Note that we useimage_tagand the methodurl_for_file_columnto display the uploaded image of the book cover, but only if it exists. We also format the fieldpublished_atto use a standard format.
Recall that George wanted the Blurb field to be easy to edit. This is why we have used the Textile markup language in the Blurb field, instead of HTML. The Textile markup we entered in the Blurb field is passed through thetextilizemethod in the view:
<%= textilize @book.blurb %>
This translates the Textile markup in the Blurb field to HTML. You’ll see this in action in the next section.
Note Thetextilizemethod is resource-intensive and should be executed only once (when the object is saved). The resulting HTML should be stored in a database field, for example,blurb_html. The conversion can easily be done using abefore_savefilter in the Book model, and then changing the view to display theblurb_htmlcolumn’s value, instead of running the conversion for each request.
Changing the Controller
There’s one more thing to fix. The view expects to find the instance variablepage_title, which means you should change the controller’sshow action, as follows:
You can now access the book details page by clicking the Show link located next to a book on the books list page. Figure 3-10 shows the page after all the changes have been done. Note that the uploaded image is shown at the bottom of the page.
Another thing to note about Figure 3-10 is that the Blurb field shows a heading, a bulleted, and a numbered list. In Figure 3-10, we entered the following into the Blurb field:
h1. This is a heading
Tip Seehttp://en.wikipedia.org/wiki/Textile_(markup_language)for more information about the Textile markup language.
Adding an Integration Test
We’ll also add an integration test for the View Book user story. This is a simple test that verifies that the page doesn’t throw an error. Add the following code to the DSL.
Theshow_bookmethod takes a book as a parameter, which it uses to call theshowaction.
Also add the highlighted line, shown in the following code, to the last line of thetest_book_administrationmethod, right after the linegeorge.list_books:
This calls the test using the book we created earlier in the test. Run the integration test again to verify that it still passes:
1 tests, 12 assertions, 0 failures, 0 errors