将select_tag中的值作为参数返回

时间:2016-03-08 21:23:56

标签: ruby-on-rails ruby

我有一个ruby表单,其中包含start_date的文本框和一个结束日期的文本框。我需要添加状态下拉列表。我添加了select_tag并正确填充。我的问题是如何获得所选的值?

表格摘录:

  <div class="modal-body">
    <div class="control-group">
      <label class="control-label">From</label>
      <div class="controls">
        <%= text_field_tag "purchase_requests_from_time", Date.today.beginning_of_month, :class => "text date_picker report_start_time_picker" %>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">To</label>
      <div class="controls">
        <%= text_field_tag "purchase_requests_to_time", Date.today.end_of_month, :class => "text date_picker report_start_time_picker" %>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">Status</label>
      <div class="controls">
        <%= select_tag :status, options_for_select(get_purchase_request_statuses) %>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">Status2</label>
      <div class="controls">
        <%= select_tag "status2", options_for_select(get_purchase_request_statuses) %>
      </div>
    </div>
  </div>

然后当我尝试访问控制器中的参数时

from_time = params[:purchase_requests_from_time]
to_time = params[:purchase_requests_to_time]
status = params[:status]
status2 = params[:status2]

时间参数显示正常,缺少状态和status2参数。

params = {ActiveSupport::HashWithIndifferentAccess} ActiveSupport::HashWithIndifferentAccess (4 elements)
 'purchase_requests_from_time' = "Tue Mar 01, 2016"
 'purchase_requests_to_time' = "Thu Mar 31, 2016"
 'action' = "export"
 'controller' = "purchase_requests"

知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。我错过了form_tag并使用了link_to而不是submit_tag。这是可行的代码

<div class="modal hide" id="download_purchase_requests">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <h3>Download Purchase Requests</h3>
  </div>
  <%= form_tag({:action => :export, :controller => :purchase_requests}, :multipart => true) do %>
  <div class="modal-body">
    <div class="control-group">
      <label class="control-label">From</label>
      <div class="controls">
        <%= text_field_tag "purchase_requests_from_time", Date.today.beginning_of_month, :class => "text date_picker report_start_time_picker" %>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">To</label>
      <div class="controls">
        <%= text_field_tag "purchase_requests_to_time", Date.today.end_of_month, :class => "text date_picker report_start_time_picker" %>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">Status</label>
      <div class="controls">
        <%= select_tag :purchase_request_status, options_for_select(get_purchase_request_statuses, :selected => params[:purchase_request_status]) %>
      </div>
    </div>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <%= submit_tag "Download", :class => "btn", :id => "download-btn", :onsubmit => "$('#download-btn').attr('disabled', 'disabled');" %>
  </div>
  <% end %>
</div>