1

我在从控制器获取一些数据后尝试通过 js.erb 渲染部分内容时遇到问题。我已经尝试了很多不同的东西,我就是无法渲染部分。日志文件显示带有 200 OK 消息的部分呈现,但页面上没有任何反应。这篇文章与我遇到的问题类似:

Rails 3 - 渲染部分 js.erb

我试图按照这篇文章的指导进行操作,但我仍然遇到问题。

这是我的部分表格 _ _left_date_filter.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 =>

    <form accept-charset="UTF-8" action="/events/dateFilter" class="date_form" data-remote="true" method="post">
    <div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="&#x2713;" />
    <input name="authenticity_token" type="hidden" value="x4cbSoYnUu2+W6qvtPnKXvXR8Tp3Yh7BP9W0RMyR02g=" /></div>
    <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" />

这是dateFilter.js.erb =>

$('#content').html("<%= escape_javascript(render(:partial => 'dateFilter')) %>");

这是events_controller =>

 def dateFilter

   eventIn = params[:event]
   start_date = Date.parse(eventIn[:start_date])
   end_date = Date.parse(eventIn[:end_date])
   sql = *redacted*

   @events = Event.find_by_sql(sql)

   respond_to do |format|
      format.html
      format.json
      format.js
  end
end

这是application.js =>

$(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) { $('#content').html(response); }
    });

 });

这是来自服务器的日志数据=>

    Started POST "/events/dateFilter" for 127.0.0.1 at 2012-12-20 12:18:57 -0500
    Processing by EventsController#dateFilter as JS
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"x4cbSoYnUu2+W6qvtPnKXvXR8Tp3Yh7BP9W0RMyR02g=", "event"=>{"start_date"=>"07/01/12", "end_date"=>"07/02/12"}, "commit"=>"Submit"}

     **shows SQL Statement getting all records**

    Rendered events/_dateFilter.html.erb (89.1ms)
    Rendered events/dateFilter.js.erb (90.7ms)
    Completed 200 OK in 212ms (Views: 80.5ms | ActiveRecord: 128.2ms)

最后,我希望呈现的页面 _dateFilter.html.erb =>

<div class="content">
<table class="table table-striped table-hover">
  <thead>
    <tr>
      <th><%= sortable  "event_id" %></th>
      <th><%= link_to "Time Created", :sort => "event_date_time_local" %></th>
      <th><%= link_to model_class.human_attribute_name(:event_type_id), :sort => "event_type" %></th>
      <th><%= link_to model_class.human_attribute_name(:severity_id), :sort => "severity_name" %></th>
      <th><%= link_to "Examined", :sort => "event_examined" %></th>
      <th><%= link_to model_class.human_attribute_name(:action_required), :sort => "action_required" %></th>
      <%#<th><%= model_class.human_attribute_name(:comments) </th>%>
      <th><%= link_to model_class.human_attribute_name(:initials), :sort => "initials" %></th>
      <th><%= link_to "Ticket ID", :sort => "taskid" %></th>
    </tr>
  </thead>
</div>

我只是不确定为什么它告诉我它正确呈现了部分,但是我的页面上没有任何反应。我对 JQuery 不是很熟悉,所以我的 application.js 或 datefilter.js.erb 文件中可能有问题。

4

1 回答 1

1

一些东西:

  • 如果ajax:remote => trueform_tag. JQuery 将自动处理对 Rails 的调用。您应该能够注释掉application.js.
  • 确保您id="content"的 HTML 中已经有一个 div。
  • 目前尚不清楚您在_dateFilter.html.erb. 你可以从一个简单的部分开始,看看它是否有效,然后你可以开始@events从控制器传递的变量中添加细节:

.

<div class="content">
 <table>
    <thead>
       <tr>
          <th>Test</th>
       </tr>
    </thead>
 </table>
</div>
于 2012-12-20T19:38:05.080 回答