Sha256: a00a255c31c537a03f0b828356d7b10cb28c0b65b73fb2e4fe4e3f33548a876e

Contents?: true

Size: 1.82 KB

Versions: 7

Compression:

Stored size: 1.82 KB

Contents

== HABTM Example

Complex forms on a HABTM association.

Using code borrowed from Pat (http://patshaughnessy.net), whose view_mapper
finally taught this hobbyist hacker to write forms.

Hopefully this will fit snugly into someone else's gem

== Installation

* Install as a gem:
    # Add to your Gemfile
    gem "anaf_habtm", :git => "git://github.com/tylergannon/anaf_habtm.git"
* or as a plugin
    rails plugin install git://github.com/tylergannon/anaf_habtm.git
    rake anaf_habtm:install  # copies some javascript to public
    
    
Add the following to your layout:
    <%= javascript_include_tag 'nested_attributes.js' %>

== Usage

Inside your model, call anaf_habtm just as you would call has_and_belongs_to_many,
except now you need to offer a code block telling rails what to do with each object.
The plugin will handle deleting stuff.

    # Basically, your code block needs to return an object of the correct
    # type for your association collection, or else nil if your code
    # determines that the object should be rejected.
    class Customer < ActiveRecord::Base
      anaf_habtm :orders, :autosave => true, :uniq => false do |params, order|
        order = order ||= Order.new
        order.attributes = params
        logger.error params.inspect
        order
      end
    end

    class Order < ActiveRecord::Base
      has_and_belongs_to_many :customers
      anaf_habtm :items, :autosave => true, :uniq => false do |params, item|
        logger.error "ITEM:::  #{params.inspect}"
        item ||= Item.find_or_create_by_name(params["name"])
      end
    end

== Additional info

See the example app at http://github.com/tylergannon/accepts-nested-attributes-habtm-example for more details.

Also check out Pat's view_mapper... it can generate the code you need inside your views.  

http://github.com/patshaughnessy/view_mapper


Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
anaf_habtm-0.0.7 README
anaf_habtm-0.0.6 README
anaf_habtm-0.0.5 README
anaf_habtm-0.0.4 README
anaf_habtm-0.0.3 README
anaf_habtm-0.0.2 README
anaf_habtm-0.0.1 README