I am trying to make a query to a db to get the available dates for that month every time a user changes the month in a jquery ui datepicker. When they select the day, it will make another query to the DB for the hours available for that day.
Should I do that? Should I change it to yearly? Would that be too many records? (I feel it would if I were to send which hours were available for which days for a year).
How do I do this correctly? When I make an Ajax call, I get "Template not found."
Relevant code:
tasks_controller.rb
def new
if signed_in?
@task = Task.new
get_dates
else
redirect_to root_path
end
end
def get_dates(day=nil)
if !day
day = DateTime.now
end
day = day.utc.midnight
minus_one_month = (day - 1.month).to_time.to_i
plus_one_month = (day + 1.month).to_time.to_i
full_days_results = AvailableDates.all.select(['unix_day, count(*) as hour_count']).group('unix_day').having('hour_count > 23').where(unix_day: (minus_one_month..plus_one_month))
full_days_arr = full_days_results.flatten
full_unix_array = []
full_days_arr.each do |full_day|
tmp_date = Time.at(full_day.unix_day).strftime('%m-%d-%Y')
full_unix_array.append(tmp_date)
end
gon.full_days = full_unix_array
return
end
tasks.js.coffee
full_days = gon.full_days if gon
$ ->
$('.datepicker').datepicker(
dateFormat: 'mm-dd-yy'
beforeShowDay: available
onChangeMonthYear: (year, month, inst) ->
target = "#{month}-01-#{year}"
jqxhr = $.get("/getdates",
day: target
)
console.log(jqxhr)
$(this).val target
return
tasks\new.html.erb
<%= f.label :end_time %>
<%= f.text_field :end_time, :class => 'datepicker' %>
routes.rb
...
match '/getdates',to: 'tasks#get_dates', via: 'get'
Error
Template is missing
Missing template tasks/get_dates, application/get_dates with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "c:/dev/rails_projects/onager-web-app/app/views"