I need some advice on whether I am taking the right approach for my web form with a twist.
I have this simple model that essentially consists of 2 fields. Here's the migration for it:
class CreateAssetAllocations < ActiveRecord::Migration
def self.up
create_table :asset_allocations do |t|
t.string :asset_allocation
t.date :effective_date
t.timestamps
end
end
end
The only twist with this model is that asset_allocation is not just a simple string. Instead, it is a list of comma-separated values (CSVs).
However, I don't want to expose the CSV formatting to the web form used for creating new instances of the model. So my 'new' view template has the following general form:
- it uses
date_selectrails helper for entering theeffective_datefield - It has an array of input objects for entering the individual components of the
asset_allocationCSV. For these, I'm using native HTML<input>tags with callouts to jQuery validation logic to make sure that the individual elements are consistent with one another. - I have native HTML
<button>element that I've created for submitting the form. It is bound to some jQuery logic which loops through the<input>fields and packs it all into a CSV string prior to sending it to the server. Once the CSV string is ready, it callsjQuery.post()with the CSV data.
Here is my button logic for submitting the form:
<script>
function okGo() {
var result=""
$('.classentry option:selected').each(function() {
var tag = $(this).val();
var value = $(this).parent().next().val();
result += tag + ":" + value + ",";
});
alert(result);
$.post("<%=security_assets_path(@security)%>", { foo: result, asset_allocation: asset_allocation } );
}
</script>
<button onClick="return okGo();">Go</button>
Here is the call to date_select (see below). You can see that I'm assigning the object to the asset_allocation variable. If you look at the call to $.post() above, you'll see that I try to include the asset_allocation object in the posted data but it doesn't work. The foo data goes fine but not the asset_allocation.
Enter Effective Date:<br />
<%= date_select("asset_allocation", "effective_date")%>
The above solution works fine for sending the CSV data to the server. However, I don't know how to get access to the date_select helper data to include in the call to $.post().
Is there a different approach that I should take for this situation?
Thanks