Skip to content

canonical-zz/OrderItemsExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

== The Canonical Order-LineItem nested form example

This example was created to demonstrate using nested forms in Rails 2.3+, the example has been kept as simple as possible so that the key elements don't get obscured by extra features.  This example shows:
    - Traditional master-detail style of form.  These show up as orders that have many line items, doctors that have many patients and so forth.
    - We will CRUD both parent and child records through a single form
    - This also demonstrates adding and removing child records using AJAX (well, no XML, so I guess it's just "AJA")

There are a few things that are conspicuously missing, like data validation and testing.  That's not the point of the example, so they were left out on purpose.

== Running the sample
    - pull the code
    - cd to the OrderItemExample directory
    - run rake db:migrate
    - run script/server -p 80
    - open a web browser to http://localhost

== How the sample was created

1. create an order model and migration:
        script/generate model -s order customer_name:string
2. Create a line_item model and migration:
        script/generate model -s line_item item_name:string price:float quantity:integer order_id:integer
        (note that we added a field called "order_id", this will be the foreign key that points to the order record)
3. We're using sqlite3, so just run the migrations
        rake db:migrate
3. Then we set up the relationship between the two models.
    In order.rb (the order model) we put:
        has_many :line_items
    In line_item.rb (the line item model) we put:
        belongs_to :order
4. A new feature in Rails 2.3 is "accepts_nested_attributes_for", this will allow us to handle the CRUD operations on the child records (line_item) automatically when we create or save the master (order) record.
    In order.rb add:
        accepts_nested_attributes_for :line_items, , :allow_destroy => true
5. Set up the routes, add these to routes.rb and also delete index.html from the public folder
        map.resources :orders
        map.root :controller => :orders
6. Create an order controller with standard restful actions (index, show, new, update, create, destroy) and basic views -- I used RyanB's nifty_scaffold and nifty_layout for this so that we would have a nice starting point for modifying the layout.
7. Add a partial for the line_items.  This needs to be one atomic "row", just the markup for that row.  It has a class="line_item" (note singular)
8. Set the ID on the element that encloses all the child records to be "line_items" (note plural)  There is no magic in these names other than they are used by the helper functions that create the "add" and "remove" links.

That's pretty much it, take a look.  Email me if you have any questions.


About

Example project, an order has_many line items

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published