0

I am creating a charts application that shows data based on a date range, and I want to have the date range default to a 1 week interval when the application is first opened, and then give the user the option to set the date range to search for a longer or shorter range. Currently I am setting the date default to what the last entered date was, so once a date is entered, it the range will persist until changed again or until going back to the home page. I am new to Ruby as a whole, so the environment is still a bit confusing, but I am thinking that it would make the most sense to set the values in the controller, but I'm not sure quite how to do that or if that is even correct. So far, I have this:

index.html.erb (view)
Start Date <%= date_field_tag :start_date, params[:start_date], id:'start', class:'dateSel' %>
End Date <%= date_field_tag :end_date, params[:end_date], id:'end', class: 'dateSel' %>

welcome_controller.rb (controller)
start_date = params["start_date"]
end_date = params["end_date"]
BeamerEA
  • 103
  • 1
  • 7

2 Answers2

1

I would add the params to the link_to that links to the page. Something like: link_to 'Chart', charts_path(start_date: (Date.today - 6.days), end_date: Date.today)

In that case when you go to the page it will always start at the past week. Then you don't need to add anything more to the controller.

Hackman
  • 1,614
  • 1
  • 12
  • 15
1

You could simply add a fallback to your view:

Start Date 
<%= date_field_tag :start_date, params[:start_date] || 1.week.ago, id: 'start', class: 'dateSel' %>
End Date 
<%= date_field_tag :end_date, params[:end_date] || Date.today, id: 'end', class: 'dateSel' %>
spickermann
  • 100,941
  • 9
  • 101
  • 131