Valencfder
New Member
I have a table that list the dates of the current month (December) 1st - 30th vertically. What I am trying to do is get the table to render the next month when the 'next month' button is clicked. In my controller I have the following: controller\[code\] def index @rota_days = RotaDay.all @hospitals = Hospital.all t1 = Date.today.at_beginning_of_month t2 = Date.today.end_of_month @dates = (t1..t2) #Concat variable t1 + t2 together @title = "Rota" respond_to do |format| format.html # index.html.erb format.json { render json: @rota_days } end end def nextmonth # Date.parse parese the full date 2012-12 is not a full date -01 is a day specification # fetches the rota days between two month string # if param[:mon]=11 se;ect * from rota_days where rota_days.date between '2012-11-01' and '2012-12-01' # sprintf("%02d") padding the date with zeros. dfrom = Date.parse("#{Date.today.year}-#{sprintf("%02d", params[:mon].to_i)}-01") dto = dfrom + 1.month @rota_days = RotaDay.where('rota_days.day BETWEEN ? AND ?', dfrom, dto) respond_with do |format| #format.json {render json: @rota_days.to_jsoninclude => {:hospital => {:include => :shortname}}) } format.json do render :json => { :dateparams => { :enday => dto.day, :month => dto.month }, :rota_days => @rota_days.to_jsoninclude => {:hospital => {:include => :shortname}})} end #An array filled with rota_day objects and every rota day has a relation to hospital. Each hospital has a #shortname method. Which is considered as seoond level association. #:include steps through everou y rota_day and calls the specified method name being :shortname end end\[/code\]index.html.erb\[code\]<table class="rota"> <thead> <tr> <th class="month" data-month="<%= @dates.first.month %>">Days</th> <% @hospitals.each do |hsp| %> <th class="hospital-<%= hsp.shortname %>"><%= hsp.name %></th> <% end %> </tr> </thead> <tbody> <% @dates.each do |d| %> <tr> <th><%= d.to_sshort) %></th> <% @hospitals.each do |hsp| %> <td class="day-<%= d.to_sshort) %> hospital-<%= hsp.shortname %>"> </td> <!--Class on each td to make it identifiable --> <% end %> </tr> <% end %> </tbody></table><%= link_to 'Next Month', rota_days_path %>\[/code\]Focusing on the \[code\]link_to\[/code\] button at the bottom of my index.html.erb is there any way of doing it so that when the \[code\]next month\[/code\] link is clicked it updates my \[code\]@dates\[/code\] variable in my controller and get the following month by doing some @dates + 1.month.