Here is the code. I am sorry because this is work I cannot post the entire application. Thanks for all help and I hope this makes my problem clearer.
Thanks,
Josh
class StressTest < ActiveRecord::Base
has_and_belongs_to_many :tst_definitions
accepts_nested_attributes_for :tst_definitions
has_many :tst_datas
end
class StressTestsController < ApplicationController
layout ‘tests.html’
def index
@stress_tests = StressTest.all
end
def new
@stress_test = StressTest.new
#want to add not create new tst_definitions
@stress_test.tst_definitions<< TstDefinition.first
end
def create
@stress_test = StressTest.new(params[:stress_test])
logger.info params.to_yaml
if @stress_test.save
flash[:notice] = “Successfully create Stress Test: #{@stress_test.name}”
redirect_to :action => ‘index’
else
render :action => ‘new’
end
end
def destroy
@stress_test = StressTest.find(params[:id])
@stress_test.destroy
redirect_to :action => ‘index’
end
end
The new form
New Stress Test
<% form_for(@stress_test) do |f| %>
<%= f.label :name %> |
<%= f.text_field :name %> |
<%= f.label :max_concurrent_scans %> |
<%= f.text_field :max_concurrent_scans %> |
<%= f.label :number_of_runs %> |
<%= f.text_field :number_of_runs %> |
<% f.fields_for :tst_definitions do |test_form| %>
- <%= test_form.select :name, TstDefinition.find_by_sql(“select id, name from tst_definitions”).collect {|test| [test.name, test.id]} %> <%= link_to ‘Remove’, ‘#remove_project’, :class => ‘remove_codesecure_project’ %>
<%= link_to ‘Add a project’, ‘
#add_project’, :class => ‘add_codesecure_project’ %>
<% end -%>
<%= f.submit 'submit' %>
<% end -%>
Javascript that addess a new drop down so that multiple tst_definitions can be added to a single stress_test
$(document).ready(function() {
$(“a[class="add_codesecure_project"]”).click(function(event){
var copy_object = $(“ul”).children().eq(0).clone()
copy_object.appendTo(“ul”);
var num_of_children = $(“ul”).children().size();
copy_object.children(“select”).attr(“id”, copy_object.children(“select”).attr(“id”).replace(/0/, (num_of_children - 1)));
copy_object.children(“select”).attr(“name”, copy_object.children(“select”).attr(“name”).replace(/0/, (num_of_children - 1)));
//alert(copy_object.children(“select”).attr(“id”).replace(/0/, (num_of_children - 1)));
//alert(copy_object.children("select").attr("id"))
//alert(copy_object.attr("id"));
add_remove_link();
});
});