Ruby on Rails,simple_form - 我想从html select_tag字段设置simple_form的隐藏字段

时间:2016-04-01 15:26:41

标签: javascript ruby-on-rails html5

我不确定是否应该在Java脚本或HTML部分中完成此操作。 我希望在HTML部分使用这样的东西:

<%= f.hidden_field :skip_option, :value => "the_value_from_the_select_tag" %>

Java Script中的工作解决方案也是可以接受的。

这是我的代码:

<%= simple_form_for(@option) do |f| %>
  <%= f.error_notification %>
  <%= link_to 'Start assessments', url_for(controller: :assessments, action: :index), id: 'start_assessment' %>
  <%= select_tag "skip_option", options_for_select([ "Yes", "No" ], "No"), id: 'skip_option', onchange: "reason_picker()" %>
  <%= f.text_field :reason_for_skipping, id: 'reason_for_skipping' %>
  <%= f.button :submit, 'Save Option' %>
<% end %>

<script type="text/javascript">
  $(document).ready (
    window.reason_picker = function () {
      var selected = document.getElementById("skip_option").selectedIndex;
      if (selected == 0) {
        $("#reason_for_skipping").show();
        $("#start_assessment").hide();
      }
      else {
        $("#reason_for_skipping").hide();
        $("#start_assessment").show();
      }
    }
  );
</script>

不知道如何将select_tag中的值分配给隐藏的表单字段。

2 个答案:

答案 0 :(得分:1)

隐藏字段的行为与其他表单输入一样,因此以下内容应该是可能的:

$("#id_of_hidden_field").val($("#skip_option").val());

答案 1 :(得分:0)

以下是基于我收到的提示的完整工作代码。 需要进行一些更改。一些元素被重命名以防止ID冲突并引入了新的隐藏元素。 同样在模型类中,使用了将空字符串转换为数据库NULL值的方法。

这是文件_form.html.erb

<%= simple_form_for(@option) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">

    <table>
      <thead>
        <th>Action</th>
        <th>Skip</th>
        <th>Reason for skipping</th>
      </thead>
      <tbody>
        <tr>
          <td>
            <%= link_to 'Start assessments', url_for(controller: :assessments, action: :index), id: 'start_assessment' %>
          </td>
          <td>
            <%= f.hidden_field :skip_option %>
            <%= select_tag "html_skip_option", options_for_select([ "Yes", "No" ], :selected => f.object.skip_option), id: 'html_skip_option', onchange: "reason_picker()" %>
          </td>
          <td>
            <%= f.text_field :reason_for_skipping, id: 'reason_for_skipping' %>
          </td>
        </tr>
      </tbody>
    </table>

  </div>

  <div class="form-actions">
    <%= f.button :submit, 'Save Option' %>
  </div>
<% end %>

<script type="text/javascript">
  $(document).ready (
    window.reason_picker = function () {
      var selected = document.getElementById("html_skip_option").selectedIndex;
      if (selected == 0) {
        $("#reason_for_skipping").show();
        $("#start_assessment").hide();
      }
      else {
        $("#reason_for_skipping").hide();
        $("#start_assessment").show();
      }
      $("#option_skip_option").val($("#html_skip_option").val());
      if ($("#option_skip_option").val() != "Yes") {
        $("#reason_for_skipping").val("");
      }
    }
  );
</script>

这是模型文件option.rb

class Option < ActiveRecord::Base
  before_save :normalize

  def normalize
    self.reason_for_skipping = nil unless skip_option == "Yes"
  end
end