0

我的任务是在 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​​ 代码进行编辑?

4

1 回答 1

0

您可以使用options_from_collection_for_select助手,它看起来像这样:

f.select(:country_id, options_from_collection_for_select(@countries, :id, :name, @selected_country_id), {:include_blank => 'Select a Country'},{:onchange=>"alert('foo')"})

我认为@countries在控制器中设置比Country.all在视图中设置更好。@selected_country_id必须设置为您实际想要选择的内容。

这里有一些关于如何设置所选选项的替代方法的文档。

于 2013-02-07T11:46:03.167 回答