我正在尝试将datepicker添加到我的应用程序中以将日期值传递给我的控制器并根据日期范围获取过滤后的记录...但我一直收到错误......
以下是我的expenses_controller操作:
def dateFilter
@expenses = current_user.expenses.where(:created_at => params[:start_date]..params[:end_date])
respond_to do |format|
format.html
format.json { render json: @expenses }
format.js
end
end
我的app / views / expenses / dateFilter.js.erb
$('.dateFilter').append('<%= escape_javascript(render(@expenses)) %>');
应用程序/资产/ Javascript角/ application.js中
//= require turbolinks
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
//= require jquery.ui.all
//= require jquery.ui.datepicker
$(document).ready(function() {
$('#date_form').submit(function (){
$.ajax({
type: 'POST',
url: "/expenses/dateFilter",
beforeSend: function (xhr) {xhr.setRequestHeader("Accept", "text/javascript");},
data: { 'start_date' : $("input[name='expense[start_date]']").datepicker(), 'end_date' : $("input[name='expense[end_date]']").datepicker()}
success: function(data) { <%= render "expenses/dateFilter" %>}
});
});
和我的dateFilter.html.erb部分如下
<%= form_tag({:controller => 'expenses', :action => 'dateFilter'}, :class => 'date_form', :remote => true) do %>
<%= datepicker_input "expense", :start_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
<%= datepicker_input "expense", :end_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
<%= submit_tag "Submit" %>
<% end %>
我的服务器日志如下
Started GET "/expenses/dateFilter" for 127.0.0.1 at 2015-01-02 15:49:39 +0530
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ExpensesController#dateFilter as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Expense Load (0.4ms) SELECT "expenses".* FROM "expenses" WHERE "expenses"."user_id" = ? AND ("expenses"."created_at" BETWEEN NULL AND NULL) ORDER BY created_at DESC [["user_id", 1]]
Rendered expenses/dateFilter.html.erb within layouts/application (90.0ms)
在304毫秒内完成500内部服务器错误
ActionView::Template::Error (undefined method `datepicker_input' for #<#<Class:0x000000037b87e0>:0x00000003500570>):
1: <%= form_tag({:controller => 'expenses', :action => 'dateFilter'}, :remote => true) do %>
2: <%= datepicker_input "expense", :start_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
3: <%= datepicker_input "expense", :end_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
4: <%= submit_tag "Submit" %>
5: <% end %>
app/views/expenses/dateFilter.html.erb:2:in `block in _app_views_expenses_date_ilter_html_erb__2248059185612722443_31189320'
app/views/expenses/dateFilter.html.erb:1:in `_app_views_expenses_date_ilter_html_erb__2248059185612722443_31189320'
app/controllers/expenses_controller.rb:42:in `dateFilter'
有人请帮助我解决我的问题...
答案 0 :(得分:0)
范围查询仅适用于DateTime对象,并且您在内部传递字符串。尝试将params转换为datetime对象,它应该可以正常工作
@expenses = current_user.expenses.where(:created_at =&gt;(params [:start_date] .to_datetime)..(params [:end_date] .to_datetime))
答案 1 :(得分:0)
您的form_tag
有:class => 'date_form'
和:remote => true
,并且应该作为javascript请求正确提交。但是application.js
中的代码
$('#date_form').submit(
这意味着date_form
不是一个类,而是一个id。
是否有其他表单以date_form
作为ID?
我建议将console.log("is this even getting executed");
放在application.js
之前
$.ajax({
type: 'POST',