如何在RubyonRails中设置select标记?

如何在RubyonRails中设置select标记?,ruby,ruby-on-rails-3,knockout.js,ruby-on-rails-3.2,Ruby,Ruby On Rails 3,Knockout.js,Ruby On Rails 3.2,我有一个任务要使用RubyonRails来击倒.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><%=

我有一个任务要使用RubyonRails来击倒.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>

禁止保存此员工:
国家 陈述 提交 $(文档).ready(函数(){ 变量位置=函数(){ var self=这个; self.country=ko.observable(); self.state=ko.observable(); }; var map=function(){ var self=这个; self.lines=ko.observearray([new location()]); self.save=函数(){ var dataToSave=$.map(self.lines(),函数(line){ 返回行。状态(){ 状态:line.state().state, 国家:line.country().country }:未定义 }); 警报(“现在可以将其发送到服务器:“+JSON.stringify(dataToSave)); $.ajax({ url:“/employees/”, 数据类型:“json”, //async:false, //contentType:'应用程序/json', 键入:“PUT”, 数据:{total_changes:JSON.stringify(dataToSave)}, //数据:JSON.stringify(dataToSave), //数据:dataToSave, 成功:功能(数据){ 警报(“成功”); }, 失败:函数(){ 警报(“未成功”); } }); }; }; 应用绑定(newmap()); });
但我想在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代码进行编辑

您可以使用来自\u集合\u的
选项\u作为\u 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
更好。必须将
@所选国家/地区id
设置为您实际想要选择的内容


关于如何设置所选选项,还有一些备选方案。

我不完全理解您的问题。你想知道,你是如何把这两部分放在一起的?希望你能理解。。我想在编辑国家和州时设置一个下拉列表。编辑页来自索引页。因此下拉列表必须具有默认值。如何将其设置为下拉列表?好的,那么您必须将
selected
属性添加到
选项
标记中,该标记应显示为默认选中。对不起,我没有理解您的意思。。你能举个例子吗?对不起,我不熟悉knockout.js。我认为您需要关于如何在Rails中使用默认选项呈现select标记的建议。但是我不能与这个RubyonRails集成,我是RubyonRails的新手。实际上,如何将javascript值设置为下拉列表?您是否尝试过选项:sampleStateCountry,选项Caption:'select',选项Text:'country',value:country%%>?