Finishing the Add Book Story for a Book Inventory Management System
(Page 1 of 2 )
In this tenth part of an ongoing series on building a book inventory management system with the scaffolding feature of Ruby on Rails, you'll learn how to finish the Add Book Story we started working on in the previous part. This article is excerpted from chapter three of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Changing the View
Next, we will use thecollection_selectview helper to generate the drop-down list for publishers. The format for thecollection_selecthelper is as follows:
<%= collection_select :book, :publisher_id, @publishers, :id, :name %>
The first and second parameters tell the helper to which model and attribute to bind the field. The third parameter is used to pass a list of publishers that should be shown in the drop-down list. The two last parameters,:idand:name, are used to specify that the value for the drop-down list should be the publisherísidand that the label should be the publisherís name.
select_tagis used for generating a list of authors from which George can select one or more authors. The format for this helper is as follows:
<%= select_tag 'book[author_ids]',
select_taghas the following parameters:
Next, change theapp/views/admin/book/_form.rhtmlfile as follows:
<%= error_messages_for 'book' %>
<p><label for="book_published_at">Published at</label><br/>
<p><label for="book_price">Page count</label><br/>
Notice that we use thetext_field, collection_select,select_tag,datetime_select, andtext_areahelpers for creating the fields. You can test the Add Book user story by first adding a couple of authors and publishers to the database, either through the user interface we created earlier or by executing the following from the console:
Then, openhttp://localhost:3000/admin/book/newin your browser. You should see a page that looks similar to Figure 3-7.
You should be able to add books to the system by entering all valid information. If you forget to enter something in a required field, you should see validation errors similar to those shown in Figure 3-8.
blog comments powered by Disqus