github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

canonical / OrderItemsExample

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 6
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Example project, an order has_many line items — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

remove dev database and server log from repo 
canonical (author)
Tue Nov 24 08:14:55 -0800 2009
commit  798a709e5c904db1680fb3be20366a163de18963
tree    afab9507b3cf8276e539d364871411eb078ada1d
parent  c22d0a6ea0329b291b4b490533b582520ba698b7
OrderItemsExample /
name age
history
message
directory .idea/ Loading commit data...
file README Mon Nov 23 23:53:44 -0800 2009 Added JS helper functions to add/remove child r... [canonical]
file Rakefile Mon Nov 23 21:12:25 -0800 2009 check in of empty project [canonical]
directory app/ Mon Nov 23 23:53:44 -0800 2009 Added JS helper functions to add/remove child r... [canonical]
directory config/ Mon Nov 23 22:35:48 -0800 2009 pluralize controller in routes [canonical]
directory db/
directory doc/ Mon Nov 23 21:12:25 -0800 2009 check in of empty project [canonical]
directory public/ Mon Nov 23 23:36:42 -0800 2009 Added display/create/edit of child records. Cl... [canonical]
directory script/
directory test/ Mon Nov 23 22:03:26 -0800 2009 1. create an order model and migration: ... [canonical]
README
== 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.


Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server