Reputation: 1090
Quick question. I am trying to set up a page that searches a database using a start date, end date and keywords. If i was to search 19-07-2012, the path would look like
search?utf8=%E2%9C%93&project%5Bstart_date%5D=19-07-2012&project%5Bend_date%5D=&%5Bstatus%5D=&keywords=
and all results would show. However when I search keywords, the search works. I can't see where I'm going wrong.
<h1>Search</h1>
<%= form_tag search_path, method: :get do %>
Start Date :
<%#= text_field_tag :start_date, params[:start_date] %></br>
<div class="field">
<%= datepicker_input "project","start_date" %></br>
</div>
End Date :
<%#= text_field_tag :end_date, params[:end_date] %></br>
<%= datepicker_input "project","end_date" %></br>
Status :
<%= select(@projects, :status, Project.all.map {|p| [p.status]}.uniq, :prompt => "-Any-", :selected => params[:status]) %></br>
Keywords :
<%= text_field_tag :keywords, params[:keywords] %></br>
<%= submit_tag "Search", name: nil %>
<% end %>
Contoller:
def search
@project_search = Project.search(params[:start_date], params[:end_date], params[:keywords]).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
EDIT:
When I search with the character "q" as a keyword, and the start date as 25-07-2012, the sql looks like this
Parameters: {"utf8"=>"✓", "project"=>{"start_date"=>"25-07-2012", "end_date"=>""}, "status"=>"", "keywords"=>"q"}
Project Load (0.4ms) SELECT "projects".* FROM "projects"
(0.2ms) SELECT COUNT(*) FROM "projects" WHERE (client LIKE '%%' AND industry LIKE '%%' AND role LIKE '%%' AND tech LIKE '%%' AND business_div LIKE '%%' AND project_owner LIKE '%%' AND exception_pm LIKE '%%' AND status LIKE '%%' AND start_date LIKE '%%' AND end_date LIKE '%%' AND keywords LIKE '%q%')
This only seems to search on the keyword "q". When I remove the keyword and leave the date in, the sql looks like:
Parameters: {"utf8"=>"✓", "project"=>{"start_date"=>"25-07-2012", "end_date"=>""}, "status"=>"", "keywords"=>""}
Project Load (0.3ms) SELECT "projects".* FROM "projects"
Thanks in advance for any help given!
Upvotes: 0
Views: 964
Reputation: 1090
I have fixed my own problem. I changed my application.js file and my search view.
Application.js:
jQuery(function(){
jQuery('#start_date').val("");
jQuery('#start_date').datepicker({dateFormat: 'dd-mm-yy'});
});
jQuery(function(){
jQuery('#end_date').val("");
jQuery('#end_date').datepicker({dateFormat: 'dd-mm-yy'});
});
Search view:
Start Date :
<%= text_field_tag("start_date") %></br>
End Date :
<%= text_field_tag("end_date") %></br>
My application now searches with a start and an end date.
Upvotes: 1
Reputation: 16834
If your code is correct, you expect to call
Project.search(start_date, end_date, keywords)
In your example, the params come in
project: {
start_date: "25-07-2012",
end_date: ""
},
keywords: "q"
But you call
Project.search(params[:start_date], params[:end_date], params[:keywords])
If your logs are correct you should be calling
Project.search(params[:project][:start_date], params[:project][:end_date], params[:keywords])
Upvotes: 0