Jeff
Jeff

Reputation: 4433

Ajax call rails returns Missing template

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"

Upvotes: 0

Views: 244

Answers (1)

Tiago Farias
Tiago Farias

Reputation: 3407

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.

Upvotes: 1

Related Questions