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



我有一个问题,我试图通过js渲染部分。在我从控制器获得一些数据之后。我已经尝试了很多不同的东西,我只是不能得到部分渲染。日志文件显示了带有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)

最后,我希望呈现的页面_dateffilter .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或dateffilter .js.erb文件可能有问题。

几点:

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

.

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

相关内容

  • 没有找到相关文章

最新更新