Rails 3 Partial不会通过js.erb渲染

时间:2012-12-20 17:26:58

标签: ruby-on-rails-3 jquery

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

Rails 3 - render partial 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 =&gt;

    <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 =&gt;

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

以下是 events_controller =&gt;

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

 });

这是来自服务器的日志数据 =&gt;

    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 =&gt;

<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文件可能有问题。

1 个答案:

答案 0 :(得分:1)

一些事情:

  • 如果您在ajax中使用:remote => true,则无需form_tag来电。 JQuery将自动处理对Rails的调用。您应该可以在application.js
  • 中注释掉代码
  • 确保您的HTML已经包含id="content"的div。
  • 目前尚不清楚在_dateFilter.html.erb中获取大量红宝石变量的位置。你能从一个简单的部分开始看它是否有效,然后你可以开始从控制器传递的@events变量中添加细节:

<div class="content">
 <table>
    <thead>
       <tr>
          <th>Test</th>
       </tr>
    </thead>
 </table>
</div>