'How to find next and previous entries

I am new on Ruby on Rails, and trying to get previous and next month entries after applying group filter. I am getting only current month entries. What mistake I did. Please let me know, how I can fix this problem. This is my controller

class TimesheetController < ApplicationController
    before_action :get_dates
    before_action :require_login
    before_action :total_working_days
    include Redmine::Export::PDF
    include Redmine::I18n
   

  def index
    @group_options = Group.all.map{ |g| [ g.name, g.id ] }
  end

  def show 
    @week_total = []
    @daily_totals = {}
    @week_matrix = []
    row = {}
   
    if (params[:group_id]  && !(params[:Next] || params[:Previous])) 
      @groups = Group.find(params[:group_id]  || params[:id]) 
      @members = @groups.users
      @time_entries = TimeEntry.where(user_id: @members).where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?",@period_start,@period_end)
      @time_entries.all.group_by(&:user_id).each do |activity, values|
        row[:percentage] = {}
        @user = User.find(activity).name
        @billable_sum = 0.0
        @nbsum = 0.0
            values.each do |value|
              if value.activity.to_s == "Billiable"
                @billable_sum = @billable_sum + value.hours
              else   
                @nbsum = @nbsum + value.hours
              end
            end
            @bpercentage = ((@billable_sum)/(8 * @working_day))*100
            @nbpercentage = ((@nbsum)/(8 * @working_day))*100
            row[:percentage][@user] = [@bpercentage.round(2), @nbpercentage.round(2)]
        values.group_by(&:issue_id).each do |issue, iv|
          row[:spent] = {}
          row[:entries] = {} 
          iv.group_by(&:spent_on).each do |day, sv|
            row[:spent][day] = sv.inject(0) { | sum, elem | sum + elem.hours }
            @daily_totals[day] = row[:spent][day] + (@daily_totals[day] || 0)        
            row[:entries] = sv
          end
          
          @week_matrix << row unless row[:spent].empty?
          row = row.dup
          end
        end
    end
        @week_total = 0.0
        (@period_start..@period_end).each do |day|
          @week_total += @daily_totals[day].to_f
        end
  end

  def grouped_issue_list(issues, query, &block)
    ancestors = []
    grouped_query_results(issues, query) do |issue, group_name, group_count, group_totals|
    while ancestors.any? &&
          !issue.is_descendant_of?(ancestors.last)
      ancestors.pop
    end
    yield issue, ancestors.size, group_name, group_count, group_totals
    ancestors << issue unless issue.leaf?
  end
end


private
  def get_dates
   
    @current_day = params.fetch(:day,Date.today).to_date 

    if params[:startdate].present?
      @period_start = @current_day.prev_month.beginning_of_month
      @period_end =  @current_day.prev_month.end_of_month
      @period_legnth = @period_end.day
      @period_shift = @period_end.day
      
    elsif params[:startdate].present?
      @period_start = @current_day.next_month.beginning_of_month
      @period_end = @current_day.next_month.end_of_month  
      @period_legnth = @period_end.day
      @period_shift = @period_end.day
      
    else
      @period_start = @current_day.beginning_of_month
      @period_end = @current_day.end_of_month 
      @period_legnth = 0
      @period_shift = @period_end.day

    end
end
    

This is my view index.html.erb

 <h3><strong>TimeSheet</strong></h3>
    
    <%= form_with(url: timesheet_show_path(@groups), method: :get, local: true) do |form| %>
        <%= form.select(:group_id, @group_options) %>
        <%= form.submit "apply" %>
    <% end%>
    

This is my show.html.erb file

<h3><strong>TimeSheet</strong></h3>
    
    <%= render :partial => 'date_range' %>
    
    
    <table class="ts-display">
                  <thead>
                  <tr>
                          <th style="min-width: 100px"><%= User %></th>
                    <th>B</th>
                    <th>NB</th>
                    <% (@period_start..@period_end).each do |d| %>
                    <th style="min-width: 45px;"> <%= I18n.t("date.abbr_day_names")[d.wday] %><br> <%= d.day %></th>
                    <% end %>
                    <th style="width: 70px;"></th>
                  </tr>
                  </thead>
                  <tbody>
                    <% @week_matrix.each_with_index do |row, idx|
                      @css_class = cycle("ts-odd", "ts-even") %>
                      <tr class="<%= @css_class %>">
                      <% row[:percentage].each do |key, entry|%>
                            <% if idx == @week_matrix.size - 1 %>
                                <td width="10px"><%= key %></td>
                            <% else %>
                                <td><%= key %></td>
                            <% end %>
                           <td><%= entry.flatten[0] %>%</td><td><%= entry.flatten[1] %>%</td>
                        <% (@period_start..@period_end).each do |d| %>
                          <% if d.wday==0 || d.wday==6%>
                              <td style="min-width: 45px; background-color: grey"><%= "hours[#{d.to_s(:param_date)}][]"
                                                    row[:spent][d].nil? || row[:spent][d] == 0.0 ?
                                                            '' : row[:spent][d] %></td>
                          <%else%>
                          <td style="min-width: 45px;"><%= "hours[#{d.to_s(:param_date)}][]"
                                                    row[:spent][d].nil? || row[:spent][d] == 0.0 ?
                                                            '' : row[:spent][d] %></td>
                          <%end%>
                        <%end%>
                      </tr>  
                      <%end%>
                    <%end%>
                  <tr><td><strong>Day Total</strong></td><td></td><td></td>
                    <% (@period_start..@period_end).each do |day| %>
                        <td><hr /><%= @daily_totals[day].to_f.round(2) %></td>
                    <%end%>
                        <td><hr /><strong><%= l(:label_total) %>:<%= @week_total.round(2) %> 
                        </strong></td>
                      </tr>
                  </tbody>
                </table>

This is my partial file.

<%= link_to "Previous", timesheet_show_path(startdate1: @period_start - 1.day) %> 
<%= I18n.t("date.month_names")[@current_day.month] %>    <%= @current_day.year%> 
<%= link_to "Next",timesheet_show_path(startdate2: @period_end + 1.day) %>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source