Skip to content

Instantly share code, notes, and snippets.

@ScottRadcliff
Created January 29, 2014 14:53
Show Gist options
  • Save ScottRadcliff/8689650 to your computer and use it in GitHub Desktop.
Save ScottRadcliff/8689650 to your computer and use it in GitHub Desktop.
$(document).ready(function(){
// Apparently I am not smart enough to figure out how to
// set the selected value on the new date_select form helper in Rails 4
// So, I had to create a client side solution
var url = document.location.href.split("?")[1];
// If the url does not have a query string
// Nothing else matters
if(url) {
var params = url.split("&");
var start_date_year = "";
var start_date_month = "";
var start_date_day = "";
var end_date_year = "";
var end_date_month = "";
var end_date_day = "";
set_param_values();
set_start_date_options();
set_end_date_options();
}
// Grab all three fields for start_date and find a match
// based on the query string. Once you find it, make it selected
function set_start_date_options() {
$("#start_date_year option").each(function(){
if ($(this).val() == start_date_year) $(this).attr("selected", "selected");
})
$("#start_date_month option").each(function(){
if ($(this).val() == start_date_month) $(this).attr("selected", "selected");
})
$("#start_date_day option").each(function(){
if ($(this).val() == start_date_day) $(this).attr("selected", "selected");
})
}
// Grab all three fields for end_date and find a match
// based on the query string. Once you find it, make it selected
function set_end_date_options() {
$("#end_date_year option").each(function(){
if ($(this).val() == end_date_year) $(this).attr("selected", "selected");
})
$("#end_date_month option").each(function(){
if ($(this).val() == end_date_month) $(this).attr("selected", "selected");
})
$("#end_date_day option").each(function(){
if ($(this).val() == end_date_day) $(this).attr("selected", "selected");
})
}
// Pull out all the query params for the data we need and save it
function set_param_values() {
$(params).each(function(k,v) {
if (v.match(/(start_date).+(year)/)) start_date_year = v.split("=")[1];
if (v.match(/(start_date).+(month)/)) start_date_month = v.split("=")[1];
if (v.match(/(start_date).+(day)/)) start_date_day = v.split("=")[1];
if (v.match(/(end_date).+(year)/)) end_date_year = v.split("=")[1];
if (v.match(/(end_date).+(month)/)) end_date_month = v.split("=")[1];
if (v.match(/(end_date).+(day)/)) end_date_day = v.split("=")[1];
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment