Reputation: 25
Okay so I need to make a search that is set up something like:
From: (Search Field) To: (Search Field)
and it only needs to show the dates equal to or in-between those fields. How would I do this?
Upvotes: 0
Views: 640
Reputation: 1082
First set your params in form_tag search
= form_tag foo_path(@foo), :method => :get do
= text_field_tag :start_date, params[:start_date]
= text_field_tag :end_date, params[:end_date]
= submit_tag "Search", :class => "btn btn-primary"
and a most important is setting your definition your params in controller, if your search are in index than
def index
@foos = Foo.all
if params[:start_date].present?
start_date = Date.parse(params[:start_date])
@foos = @foos.where("foo.foo_date >= ?", start_date)
end
if params[:end_date].present?
start_date = Date.parse(params[:end_date])
@foos = @foos.where("foo.foo_date >= ?", end_date)
end
end
Upvotes: 0
Reputation: 412
You can use range for this kind of search. For example:
User.where(registred_at: from_field..to_field)
Upvotes: 0
Reputation: 5964
You can write condition like below,
User.where("created_at >= ? and end_date <= ?",from_date, end_date),where
from_date,end_date --> are your required from_date and end_date stored in variables
Hope it helps!!
Upvotes: 0
Reputation: 115511
This is written in Rails guides, para 2.2.1:
Client.where("created_at >= :start_date AND created_at <= :end_date", {:start_date => params[:start_date], :end_date => params[:end_date]})
Upvotes: 4