I currently have the below working properly in my application:
Form
<%= f.fields_for :types do |builder| %>
<p>
<%= builder.select(:name, [['Type A', 'Type A'],
['Type B', 'Type B'],
['Type C', 'Type C'],
['Type D', 'Type D'],
['Type E', 'Type E']
],{ :prompt => "Please select"},
{ :multiple => true, :size => 5 }
) %>
</p>
% end %>
Controller
def new
@task = Task.new
1.times { @task.types.build }
end
Type Model
class Type < ActiveRecord::Base
belongs_to :task
accepts_nested_attributes_for :task
attr_accessible :name, :task_attributes
end
Task Model
class Task < ActiveRecord::Base
has_many :types, :dependent => :destroy
accepts_nested_attributes_for :types
attr_accessible :types_attributes
end
My problem is that when submitted the information all goes into the same row and 'name" field in the types table -- I need it to create separate rows in the types table.
I need this solution http://forums.phpfreaks.com/topic/195729-multiple-select-drop-down-data-saved-to-mysql-how/ but for Rails
I can get separate rows created using text fields with the below code:
Form
<%= f.fields_for :types do |builder| %>
<p>
<%= builder.label :name, "Type Name" %><br />
<%= builder.text_field :name %>
<%= builder.check_box :_destroy %>
<%= builder.label :_destroy, "Remove Chore" %>
</p>
<% end %>
Controller
def new
@task = Task.new
3.times { @task.types.build }
end
Any assistance wold be appreciated.