Question

I have a date range selector for a report page which uses jQueryUI's datepicker. I have two fields for the start date and two fields for the end date:

  <%= form_tag stats_path, :method => 'get', :id => "date-range" do %>
    <%= hidden_field_tag :start_date %>
    <%= text_field_tag :start_date_display %>
    <%= hidden_field_tag :end_date %>
    <%= text_field_tag :end_date_display %>
    <%= submit_tag "Run it!" %>
  <% end %>

Currently, as expected, all 4 fields are added to the query string as parameters, but I only want the hidden fields to be sent to the server. The text fields are just to display the dates in a user friendly format (e.g., May 5th) when the user selects a date.

Is there a way to exclude them?

I can't use :disabled => true, which would exclude them, because it makes the control unusable.

(I like having the date ranges in the query string so you can easily link to a report for a given range.)

Was it helpful?

Solution

Here's how I solved it using CoffeeScript:

  $('#date-range').submit ->
      $(this).find('input[id*="display"]').each ->
          $(this).remove()

But an even better solution posed by @StuR is to set an empty value for the name attribute:

  <%= form_tag stats_path, :method => 'get', :id => "date-range" do %>
    <%= hidden_field_tag :start_date %>
    <%= text_field_tag :start_date_display, '', :name => nil %>
    <%= hidden_field_tag :end_date %>
    <%= text_field_tag :end_date_display, '', :name => nil %>
    <%= submit_tag "Run it!" %>
  <% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top