Database Articles

  Home arrow Database Articles arrow Finishing the Add Book Story for a Boo...
DATABASE ARTICLES

Finishing the Add Book Story for a Book Inventory Management System
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2011-11-23

    Table of Contents:
  • Finishing the Add Book Story for a Book Inventory Management System
  • Updating the Integration Test

  •  
     

    SEARCH CODEWALKERS

    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][]',
    options_from_collection_for_select(@authors, :id, :name,➥@book.authors.collect{|author| author.id}),
    { :multiple => true, :size => 5 }
    %>

    select_taghas the following parameters:

    1. The first parameter,book[author_ids][], specifies to which attribute the field should be bound. Note that the parameter must end with[]so that Rails knows that the attribute it needs to bind the value to is an array.
    2. The second parameter,options_from_collection_for_select, is used for generating the list of options that should be shown in the list. This methodís first parameter is a collection of authors. The methodís second and third parameters,:idand:name, specify the attributes that should be used as the value and label, respectively. The fourth parameter is used for preselecting the authors that have been assigned to the book.
    3. The third parameter of theselect_tagis used for specifying options. In this case, we specify that the list should support multiple-selections and that five authors should be shown.

    Next, change theapp/views/admin/book/_form.rhtmlfile as follows:

    <%= error_messages_for 'book' %>

    <p><label for="book_title">Title</label><br/>
    <%= text_field 'book', 'title' %></p>

    <p><label for="book_publisher">Publisher</label><br/>
    <%= collection_select :book, :publisher_id, @publishers, :id, :name %></p>

    <p><label for="book[author_ids][]">Authors</label><br/>
    <%= select_tag 'book[author_ids][]',
    options_from_collection_for_select(@authors, :id, :name, ➥
    @book.authors.collect{|author| author.id}),
    { :multiple => true, :size => 5 }
    %>
    </p>

    <p><label for="book_published_at">Published at</label><br/>
    <%= datetime_select 'book', 'published_at' %></p>

    <p><label for="book_isbn">Isbn</label><br/>
    <%= text_field 'book', 'isbn' %></p>

    <p><label for="book_blurb">Blurb</label><br/>
    <%= text_area 'book', 'blurb' %></p>

    <p><label for="book_price">Price</label><br/>
    <%= text_field 'book', 'price' %></p>

    <p><label for="book_price">Page count</label><br/>
    <%= text_field 'book', 'page_count' %></p>

    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:

    $ script/console

    --------------------------------------------
    Loading development environment.
    >> Publisher.create(:name => 'Apress')
    >> Author.create(:first_name => 'Salman', :last_name => 'Rushdie')
    >> Author.create(:first_name => 'Joel', :last_name => 'Spolsky')
    --------------------------------------------

    Then, openhttp://localhost:3000/admin/book/newin your browser. You should see a page that looks similar to Figure 3-7.


    Figure 3-7. The add book page with publishers and authors

    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.


    Figure 3-8. Validation errors

    More Database Articles Articles
    More By Apress Publishing

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - MariaDB, a MySQL Alternative, Opens Foundati...
    - Oracle Supporting HP Itanium Again, Finally
    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap