I am a beginner in Ruby on Rails. I have a feedback model. I want to search the feedback database so that I get results within a selected date range using the created_at field. So I type e.g. "14/04/15" in one box and 16/04/15" and that when I click search, it brings up the @feedbacks between the typed dates, I will later on add jquery date-picker to select the dates in the text fields but I need the thing to function first.
I'm sorry if this has been answered before but nothing seems to help me. I have a form in my feedbacks index:
<%= form_tag(feedbacks_path, :method => "get") do %>
<%= text_field_tag :start_date, params[:start_date] %>
<%= text_field_tag :end_date, params[:end_date] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
and this in my feedback controller:
def index
@feedbacks = Feedback.where("created_at >= :start_date AND created_at <= :end_date",
{start_date: params[:start_date], end_date: params[:end_date]})
@feedbacks = Feedback.where(user_id: current_user)
end
URL:
http://localhost:3000/feedbacks?utf8=%E2%9C%93&start_date=14%2F04%2F2015&end_date=16%2F04%2F2015
Gemfile:
gem 'rails', '4.1.6'
ruby 2.1.3p242
Aucun commentaire:
Enregistrer un commentaire