RubyonRailsを使用してknockout.jsを実行するタスクがあります。私の実際のhtmlコードは
<%= javascript_include_tag "knockout-2.2.0","country-state" %>
<%= form_for(@employee) do |f| %>
<% if @employee.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@employee.errors.count, "error") %> prohibited this employee from being saved:</h2>
<ul>
<% @employee.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<table>
<thead>
<tr>
<th>Country</th>
<th>State</th>
<th> </th>
</tr>
</thead>
<tbody data-bind='foreach: lines'>
<tr>
<td>
<select data-bind='options: sampleStateCountry,optionsCaption: "select", optionsText: "country", value: country'> </select>
</td>
<td data-bind="with: country">
<select data-bind='options: state, optionsText: "state", optionsCaption: "select", value: $parent.state'> </select>
</td>
</tr>
</tbody>
</table>
<button data-bind='click: save'>Submit</button>
<% end %>
<script>
$(document).ready(function() {
var location = function() {
var self = this;
self.country = ko.observable();
self.state = ko.observable();
};
var map = function() {
var self = this;
self.lines = ko.observableArray([new location()]);
self.save = function() {
var dataToSave = $.map(self.lines(), function(line) {
return line.state() ? {
state: line.state().state,
country: line.country().country
} : undefined
});
alert("Could now send this to server: " + JSON.stringify(dataToSave));
$.ajax({
url: '/employees/<%=@employee.id%>',
dataType: 'json',
//async: false,
//contentType: 'application/json',
type: 'PUT',
data: {total_changes: JSON.stringify(dataToSave)},
//data:JSON.stringify(dataToSave),
//data:dataToSave,
success: function(data) {
alert("Successful");
},
failure: function() {
alert("Unsuccessful");
}
});
};
};
ko.applyBindings(new map());
});
</script>
しかし、私はこれをこのようにルビーに設定したいと思います。
<%= form_for(@employee) do |f| %>
<% if @employee.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@employee.errors.count, "error") %> prohibited this employee from being saved:</h2>
<ul>
<% @employee.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class='label'>
<%= f.label :country %><br />
<%= f.text_field :country %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
これは一例です。編集用のルビーコードとして設定するにはどうすればよいですか?