0

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.

  1. 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).

  2. 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"
Jeff
  • 4,285
  • 15
  • 63
  • 115
  • as far as I know, you need to render something, e.g `render :json => gon.full_days.to_json`. Also, `def get_dates(day=nil)`, what you need will be in the `params` array, e.g. `params[:day]` – martincarlin87 Jun 26 '14 at 15:45

1 Answers1

1

Well, couple of things you need to do:

1 - Make get_dates private (and choose a more ruby-like name)

private

def hours_for(day=nil) 
...
end

Rails is thinking get_dates is a controller action when it's really not. That's why it cannot find the respective view for the get_dates action (when it's not an action, it's a helper method, maybe you should consider putting it in a model)

2 - hour_for method should return something. Right now it's not. I don't know what this line does:

gon.full_days = full_unix_array

I mean, what is gon? Just return the array directly. You shouldn't be setting stuff in get methods. Also, take a look at this to learn how to render json in rails pages.

3 - Rename your tasks.rb to tasks_controller.rb in your controllers folder in your rails project.

4 - Fix the routes.rb file to:

get '/getdates/:day', to: 'tasks#load_dates', as: 'getdates'

Also, hours_for must be called at load_dates. Your 'new' action in tasks should render a template and every time the user updates the date, your coffeescript should call the load_dates ajax method.

Now, what you need to do is learn how to update your new.html.erb page.

Community
  • 1
  • 1
Tiago Farias
  • 3,397
  • 1
  • 27
  • 30
  • gon is setting a js variable... I've edited my question to show this. – Jeff Jun 26 '14 at 15:42
  • tasks is tasks_controller... mistake while posting to SO... sorry. – Jeff Jun 26 '14 at 15:54
  • If I move hours_for (or whatever I call it) to tasks.rb (the model) or tasks_helper.rb (not 100% sure what this is) how can I call it from the view / ajax? Forgive my ignorance... I'm still new to this. – Jeff Jun 26 '14 at 15:55
  • 1
    Don't move things just yet. Try to make it work first. Then refactor to a better way. – Tiago Farias Jun 26 '14 at 16:01
  • So, when I get it to return a value, I want to access the responseJSON? – Jeff Jun 26 '14 at 16:47
  • Yeah, right. I don't understand coffee script much. But generally you get the response JSON in a callback and change the page accordingly. – Tiago Farias Jun 26 '14 at 18:02