【问题标题】:Ajax call rails returns Missing templateAjax 调用 rails 返回 Missing 模板
【发布时间】:2014-06-26 15:19:01
【问题描述】:

每次用户在 jquery ui datepicker 中更改月份时,我都会尝试对数据库进行查询以获取该月的可用日期。当他们选择日期时,它将再次向数据库查询当天的可用时间。

  1. 我应该这样做吗?我应该将其更改为每年吗?会不会记录太多? (我觉得如果我要发送一年中哪些日子有哪些小时可用)。

  2. 如何正确执行此操作?当我进行 Ajax 调用时,我收到“找不到模板”。

相关代码:

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

任务\new.html.erb

<%= f.label :end_time %>
<%= f.text_field :end_time, :class => 'datepicker' %>

routes.rb

...
match '/getdates',to: 'tasks#get_dates',      via: 'get'

错误

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"

【问题讨论】:

  • 据我所知,你需要渲染一些东西,例如render :json =&gt; gon.full_days.to_json。此外,def get_dates(day=nil),您需要的将在 params 数组中,例如params[:day]

标签: javascript jquery ruby-on-rails ruby ajax


【解决方案1】:

嗯,你需要做几件事:

1 - 将 get_dates 设为私有(并选择一个更像 ruby​​ 的名称)

private

def hours_for(day=nil) 
...
end

Rails 认为 get_dates 是控制器操作,但实际上并非如此。这就是为什么它找不到 get_dates 动作的相应视图(当它不是动作时,它是一个辅助方法,也许你应该考虑将它放入模型中)

2 - hour_for 方法应该返回一些东西。现在不是。我不知道这条线是做什么的:

gon.full_days = full_unix_array

我的意思是,gon 是什么?直接返回数组即可。你不应该在 get 方法中设置东西。另外,查看this 以了解如何在 Rails 页面中呈现 json。

3 - 在 Rails 项目的控制器文件夹中将 tasks.rb 重命名为 tasks_controller.rb

4 - 将 routes.rb 文件修复为:

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

另外,hours_for 必须在 load_dates 调用。您在任务中的“新”操作应该呈现一个模板,并且每次用户更新日期时,您的咖啡脚本应该调用 load_dates ajax 方法。

现在,您需要学习如何更新您的 new.html.erb 页面。

【讨论】:

  • gon 正在设置一个 js 变量...我已经编辑了我的问题以显示这一点。
  • tasks 是 tasks_controller...发帖到 SO 时出错...抱歉。
  • 如果我将 hours_for (或我称之为的任何名称)移动到 tasks.rb (模型)或 tasks_helper.rb (不是 100% 确定这是什么)我如何从视图 / ajax 中调用它?原谅我的无知...我还是新手。
  • 暂时不要移动东西。试着让它先工作。然后重构为更好的方式。
  • 那么,当我让它返回一个值时,我想访问 responseJSON?
猜你喜欢
  • 2014-01-29
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 2015-06-25
  • 2015-04-14
相关资源
最近更新 更多