我是Rails和JQuery的新手,所以我会尽力解释这个问题。我正在尝试将我的JQuery datepicker值传递给我的rails控制器。该页面上有数据,我希望用户能够根据日期范围进行过滤。关于这个主题有很多不同的帖子:
Passing the variables from from jquery to rails controller
How to pass variables from AJAX form into controller?
我试图遵循这些帖子的指导,但我仍然遇到麻烦。
这是我的部分_ dateFilter.html.erb =>
<%= form_tag({:controller => 'events', :action => 'dateFilter', :class => 'date_form'}, :remote => true) do %>
<%= datepicker_input "event", :start_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
<%= datepicker_input "event", :end_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
<%= submit_tag "Submit" %>
<% end %>
以下是生成的 HTML =&gt;
<form accept-charset="UTF-8" action="/events/dateFilter?class=date_form" data-remote="true" method="post">
<input class="dateFilter" id="event_start_date" name="event[start_date]" size="30" type="text" /><script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){jQuery('#event_start_date').datepicker({"dateFormat":"mm/dd/y"})});
//]]>
</script>
<input class="dateFilter" id="event_end_date" name="event[end_date]" size="30" type="text" /><script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){jQuery('#event_end_date').datepicker({"dateFormat":"mm/dd/y"})});
//]]>
</script>
<input name="commit" type="submit" value="Submit" />
</form>
这是 dateFilter.js.erb =&gt;
$('.dateFilter').append('<%= escape_javascript(render(@events)) %>');
以下是 events_controller =&gt;
def dateFilter
@events = Event.find(:all, :conditions => ["EVENT_DATE_TIME_LOCAL BETWEEN ? AND ?", params[:start_date], params[:end_date]])
respond_to do |format|
format.html
format.json { render json: @events }
format.js
end
end
最后这里是 application.js =&gt;
$(document).ready(function() {
$('#date_form').submit(function (){
$.ajax({
type: 'POST',
url: "/events/dateFilter",
beforeSend: function (xhr) {xhr.setRequestHeader("Accept", "text/javascript");},
data: { 'start_date' : $("input[name='event[start_date]']").datepicker(), 'end_date' : $("input[name='event[end_date]']").datepicker()}
success: function(data) { <%= render "events/dateFilter" %>}
});
});
这是来自服务器的日志数据 =&gt;
Started POST "/events/dateFilter?class=date_form" for 127.0.0.1 at 2012-12-12 15:09:39 -0500
Processing by EventsController#dateFilter as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"J/HU+DxNmIwEhu3keN071k6MGCCD/n/1UhXUD0MkH1Q=", "event"=>{"start_date"=>"12/06/12", "end_date"=>"12/13/12"}, "commit"=>"Submit", "class"=>"date_form"}
Event Load (1.7ms) SELECT "TBLEVENT".* FROM "TBLEVENT" WHERE (EVENT_DATE_TIME_LOCAL BETWEEN NULL AND NULL)
Rendered collection (0.0ms)
Rendered events/dateFilter.js.erb (0.3ms)
Completed 200 OK in 8ms (Views: 3.4ms | ActiveRecord: 1.7ms)
其中一个问题是我一直在为我想要传递的数据值获取“NULL”。我甚至不确定我是否正确地对待这一点,对此的任何帮助/反馈将不胜感激。感谢。
答案 0 :(得分:1)
根据您的日志数据,这是Parameters
:
"event"=>{"start_date"=>"12/06/12", "end_date"=>"12/13/12"}
表示start_date
和end_date
位于名为event
的哈希中,因此您的控制器应如下所示:
def dateFilter
eventIn = params[:event]
@events = Event.find(:all, :conditions => ["EVENT_DATE_TIME_LOCAL BETWEEN ? AND ?", eventIn[:start_date], eventIn[:end_date]])
respond_to do |format|
format.html
format.json { render json: @events }
format.js
end
end
另一件小事 - form_tag
class
应如此设置:
<%= form_tag({:controller => 'events', :action => 'dateFilter'}, :class => 'date_form', :remote => true) do %>