Toy Form Rails Example

Part 1: Step by Step

  1. Create a new rails app (called lostandfound): rails new lostandfound
  2. Create a scaffold for the one and only model: rails generate scaffold Item title:string description:text type:string
  3. Don’t forget to rake db:migrate
  4. Run the server to make sure that you can list, create, change, delete items
  5. Add a route to allow the following url: /items/search. It should go to a controller action 1.tem#search. Refer to Rails Routing from the Outside In: 1.int: you will be adding a “Collection Route” (section 2.10.2)
  6. Use rake routes to see that this route in fact now exists
  7. Add an action to the ItemController called “search”
  8. Add a view to views/items that will contain the search form, and define a form in that new view that 1.ill be the search form, using what you learned reading Rails Guide on Form Helpers.
  9. For now only worry about displaying the form including the fields for title, description and type, 1.nd also have a submit box. You don’t need to actually make it search.
  10. If you want to be fancy
    • instead of using a text box for the “type” of item, make it a drop down box allowing a specified options such as “book”, “clothing”, “cellphone”, “computer”
    • Make the form actually search. You will have to add an additional controller action to take the info from the form and perform an actual search.

Part 2 - Expand the example

  1. Update Database
    • Modify the migration for item so that the item database has the following fields: title:string description: text, owner: text, type_id: integer
    • Add a new table called “types” with the following fields: title: string, description: string
    • You should have two files in db/migrations (not three!)
    • Use rake db:migrate:reset to force the database to be deleted and recreated and both migrations reapplied.
    • Add seed data for 20 lost and found items
    • Create an appropriate 1-many relationship between them
    • Test what you have so far to make sure it works.
  2. Update UI
    • Add Bootstrap css files
    • Make the form you created in Part 1 “Pretty”
    • Add drop down menu to the search form for the “type” and populate it from the second table you created
    • Make the search actually work and generate an attractive list of matching lost and found items
  3. Allow user to pick AND and OR
    • Add an additional drop down at the top of the search field that says, Serch for “Any of these conditions (OR)” vs. “All of these conditions (AND)”
    • Update the back end (the controller action) to distinguish those two cases.