A quick overview of MVC might help here.
Model - the first level above the database, controls attributes of objects (e.g. Project has a date)
Controller - the interaction between Models and Views (and routes, but set that aside)
View - the interaction between the browser and the Controller
You want to have some "filtering" buttons on the index view.
Clicking this button will either:
Take the browser to a new URL
Trigger some javascript that will manage the context of the current view
Let me get you headed in the right direction for each of these options:
Option 1A: New views for each button
You could structure your controller something like this:
class ProjectsController < ApplicationController
def index
@projects = Project.all
end
def today
@projects = Project.where('date BETWEEN ? AND ?', DateTime.now.beginning_of_day, DateTime.now.end_of_day).all
end
def this_week
@projects = Project.where('date BETWEEN ? AND ?', DateTime.now.beginning_of_day + 1, DateTime.now.end_of_day + 6).all
end
Then make routes that match:
Rails.application.routes.draw do
...
resources :projects do
get 'today', on: :collection
get 'this_week', on: :collection
end
...
You'll also need to create views: today.html.erb
and this_week.html.erb
Users will click a button and be taken to projects/today
or projects/this_week
views.
OPTION 1B: allow your index to handle parameters filtering
You could structure your controller something like this:
class ProjectsController < ApplicationController
def index
start_date = params[:start] || DateTime.now.beginning_of_day
end_date = params[:end] || DateTime.now.end_of_day
@projects = Project.where('date BETWEEN ? AND ?', start_date, end_date).all
end
then customize your buttons:
<%= button_to 'Today', projects_path() %>
<%= button_to 'This week', projects_path(start: DateTime.now.beginning_of_week, end: DateTime.now.end_of_week) %>
<%= button_to 'This year', projects_path(start: DateTime.now.beginning_of_year, end: DateTime.now.end_of_year) %>
This will re-load the Projects#index
with parameters in the URL, like:
"https://example.com/projects/?start=2019-01-01&end=2019-12-31"
And you can access those parameters to intelligently filter your projects.
Option 2: Javascript & AJAX
This one is complicated. I recommend checking out some tutorials. Like this one:
https://www.rubyguides.com/2019/03/rails-ajax/