編集:これを探しています: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (これが機能する場合、私は自分の質問に答えます)
formtastic を使用して、in and out タイプの 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 %>