I have a rails application where I display some results based on a date range. I am using some global variables to set the dates. My code looks like below:
#index.html.erb
<td>From: <%= $from %></td>
<td>To: <%= $to - 1.day%></td>
#controller
if (params[:first] || params[:next] || params[:previous])
if params[:first]
$from = Date.today - 7.day
$to = Date.today + 1.day
end
if params[:next]
$from = $from - 7.day
$to = $to - 7.day
end
if params[:previous]
if $to != Date.today + 1.day
$to = $to + 7.day
$from = $from + 7.day
end
end
end
This code seems to work fine, but when I access the application from more than one machine, it is a mess as the global variables are not unique for each instance I guess.
So I tried to use @variable (instance variables) instead of global variables. But whenever I submit the page from the view, all the variables get reset to NULL and the values don't persist to the controller. Can someone suggest me some pointers to deal with the problem here.