-1

On my index page displays all the projects from the database.

@projects = Project.all

But I also want to have some buttons that could change this list.I have methods for this. Like:

  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

How can I add these methods to buttons and when I click change the display of the list of projects?

namename
  • 11
  • 1
  • Where's your button code which shows what you have tried so far? This might help btw: https://stackoverflow.com/questions/17243200/how-to-call-a-controller-method-from-a-button-in-rails-4 – Christian Nov 17 '19 at 20:16
  • here is my button: <%= button_to 'Call Action', home_today_path, method: :post, remote: true %>. I also added this to my routes post 'home/today'. BUT these actions do not change @projects – namename Nov 17 '19 at 20:23
  • 2
    Where do these methods live? In the controller? In the model? – Chiperific Nov 17 '19 at 20:24
  • In the controller – namename Nov 17 '19 at 20:25
  • This might help you: https://stackoverflow.com/questions/11297623/custom-post-routes-for-create-action-not-fired-up – Christian Nov 17 '19 at 20:30

1 Answers1

1

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:

  1. Take the browser to a new URL

  2. 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/

Chiperific
  • 4,428
  • 3
  • 21
  • 41