0

编辑:寻找这个:http ://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (如果这有效,我会回答我自己的问题)

我已经开始通过formtastic创建一个进出型habtm(有很多并且属于很多)形式。但是我希望它对用户更灵活,这意味着如果您在一侧选择一个名称,您可以单击一个立即移动它的按钮(点击 ajax)或在提交表单后

(我使用的是formtastic,但在任何人的例子中都不需要)

我正在努力解决的是javascript,输入和输出按钮..

比较模型

class Comparison < ActiveRecord::Base
    has_and_belongs_to_many :devices
end

设备型号

class Device < ActiveRecord::Base
    has_and_belongs_to_many :comparisons
end

比较控制器

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

比较编辑视图

<% 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 %>
4

1 回答 1

1

我将使用 jquery 来解决这个问题:

http://quasipartikel.at/2009/05/10/jqueryui-multiselect/

或者

http://blog.jeremymartin.name/2008/02/easy-multi-select-transfer-with-jquery.html#

于 2011-02-26T03:30:09.653 回答