views:

148

answers:

0

EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question)

I've started to create an in and out type habtm (has many and belongs to many) form through formtastic. However I would like it to be more flexible to the user, meaning if you select a name on one side you either click a button that moves it right away (ajax on click) or after the form is submitted

(I'm using formtastic with this, but it doesn't have to in anyones examples)

What I am struggling with is the javascript, in and out buttons..

Comparison Model

class Comparison < ActiveRecord::Base
    has_and_belongs_to_many :devices
end

Devices Model

class Device < ActiveRecord::Base
    has_and_belongs_to_many :comparisons
end

Comparison Controller

def edit
  @comparison = Comparison.find(params[:id])
  @devices = Device.find(:all, :select => 'id, device_name', :order => 'device_name')

  @devices_selected = @comparison.devices.find(:all, :order => 'device_name', :conditions => ["id IN (?)", @devices])

  @devices_not_selected = Device.find(:all, :order => 'device_name', :conditions => ["id NOT IN (?)", @devices_selected])
end

Comparison Edit View

<% semantic_form_for @comparison do |f| %>
 <% f.inputs do %>
    <%= f.input :comparison_name %>
    <%= f.input :description %>
    <h3>
        Select Devices
    </h3>
    <% f.inputs :class => 'inline_fields' do %>
        <%= f.input :devices,
                    :collection => @devices_not_selected,
                    :label_method => :device_name,
                    :label => 'Add Devices',
                    :input_html => { :size => 20 },
                    :include_blank => false,
                    :hint => 'Select devices from this list to compare to the parent device' %>
        <%= f.input :devices,
                    :collection => @devices_selected,
                    :label_method => :device_name,
                    :label => 'Remove Devices',
                    :input_html => { :size => 20 },
                    :include_blank => false,
                    :hint => 'Deselect devices from this list to take them off the comparison' %>
    <% end %>
 <% end %>
<%= f.buttons %>
<% end %>