Sha256: b5ed452d393250db3201ce1181dec0a3cd5121cc9e932389dcd4694c733873b4

Contents?: true

Size: 1.79 KB

Versions: 43

Compression:

Stored size: 1.79 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"
* or as a plugin
    rails plugin install git://github.com/tylergannon/anaf_habtm.git

Run the generator to place the javascript in the right place:

    rails generate anaf_habtm
        
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

43 entries across 43 versions & 2 rubygems

Version Path
casey_jones-0.1.2 README
casey_jones-0.1.1 README
casey_jones-0.1.0 README
casey_jones-0.0.121 README
casey_jones-0.0.120 README
casey_jones-0.0.119 README
casey_jones-0.0.118 README
casey_jones-0.0.117 README
casey_jones-0.0.116 README
casey_jones-0.0.115 README
casey_jones-0.0.114 README
casey_jones-0.0.113 README
casey_jones-0.0.112 README
casey_jones-0.0.111 README
casey_jones-0.0.110 README
casey_jones-0.0.109 README
casey_jones-0.0.108 README
casey_jones-0.0.107 README
casey_jones-0.0.106 README
casey_jones-0.0.105 README