如何在ruby on rails中设置select标记



我有一个任务要使用ruby on rails敲除.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>

但我想把这个设置成这样的ruby。

<%= 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代码进行编辑?

您可以使用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必须设置为您实际想要选择的内容。

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

相关内容

  • 没有找到相关文章

最新更新