我的任务是在 rails 上使用 ruby 来敲除.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 %>
这是一个例子。如何将其设置为 ruby 代码进行编辑?