I’m trying to search on a date, and the format in my database is, 2012-05-15. When I use datepicker, I have it so that the user can pick a date and it is then displayed as 15-05-2012.
I’m wanting to search on the 2012-05-15 format, without changing the way the viewer sees it. I tried to change format in the model, but had no luck.
Here’s my search view:
<%= form_tag search_path, method: :get do %>
<%= text_field_tag("start_date") %></br>
<%= submit_tag "Search", name: nil %>
<% end %>
Here’s my application.js:
jQuery(function(){
jQuery('#start_date').val("");
jQuery('#start_date').datepicker({dateFormat: 'dd-mm-yy'});
});
My Controller:
def search
@project_search = Project.search( params[:start_date].order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)
@project = Project.new(params[:project])
respond_to do |format|
format.html # search.html.erb
format.json { render :json => @project }
end
end
and my model:
def self.search(search_start_date)
return scoped unless search_start_date.present?
where([start_date LIKE ?, "%#{search_start_date}%"])
end
I tried using jquery options, but had no luck.
jQuery(function(){
jQuery('#start_date').val("");
jQuery('#start_date').datepicker({altField: '#start_date2', dateFormat: 'yy-mm-dd'});
});
Hopefully someone can help me out. Thanks in advance!
Changing
dateFormatin the jQuery Datepicker instance options is going to change the way your users see the date.In your model
Validating
search_start_date‘s initial format to ensure it’s otherwise should be considered too.