【问题标题】:Trouble refreshing partial using AJAX and Rails使用 AJAX 和 Rails 刷新部分时出现问题
【发布时间】:2012-11-15 22:04:33
【问题描述】:

我是 Web 开发的新手,我目前正在为 Rails 开发一个简单的项目。现在,我有一个数据库,其中的条目都有一个条目日期,我有一个索引页面,显示本周的所有条目,效果很好。

下一步是实现一个方法让用户选择一周(当前使用jQuery UI datepicker)然后单击一个按钮,该按钮会自动刷新索引页面上的部分以显示该周的条目。

这是我目前所拥有的:

在 /app/views/reports/index.html.erb 中

<script>$(function() {
  $("#datepicker").datepicker();
  $("#datepicker").val($.datepicker.formatDate('mm/dd/yy', new Date()));
});</script>
<% require 'date' %>
<h1>Listing reports</h1>

<input type="datetime" id="datepicker" /> <%= button_to "Select Date", :action =>     'showEntries', :remote => 'true' %>

<div id="displayReports">
<%= render 'display', :date => Date.today %>
</div>

<br />

<%= link_to 'New Report', new_report_path %>
<%= link_to 'Send Weekly Email', weekly_email_reports_path %>

在 /app/views/reports/_display.html.erb

<% require 'date' %>
<table>
  <tr>
   <th>App</th>
    <th>Week</th>
    <th>Completed</th>
    <th>Planned</th>
    <th>Releases</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @reports.each do |report| %>
<% if report.entrydate.strftime('%U') == date.strftime('%U') %>
  <tr>
    <td><%= report.app %></td>
    <td><%= report.entrydate %></td>
    <td><%= report.completed %></td>
    <td><%= report.planned %></td>
    <td><%= report.releases %></td>
    <td><%= link_to 'Show', report %></td>
    <td><%= link_to 'Edit', edit_report_path(report) %></td>
    <td><%= link_to 'Destroy', report, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
  </tr>
<% end %>
<% end %>
</table>

在 /app/controllers/reports/showEntries.js.erb 中

var selectDate = $("#datepicker").val();
$("#displayReports").html("<%= escape_javascript(render(:partial => 'display', :locals => { :date => " + selectDate + " })).html_safe %>");

在 /config/routes.rb 中

Summary::Application.routes.draw do
  resources :reports do
    collection do
      get 'weekly_email'
      post 'showEntries'
    end
  end

  get "home/index"
end

当我尝试运行页面时,我得到了错误:

No route matches {:action=>"showEntries", :remote=>"true", :controller=>"reports"}

很多类似于这个问题的问题并没有真正帮助我。如果你能指出我做错了什么或者一个网站可以阅读 Rails AJAX 调用,那就太好了。谢谢。

【问题讨论】:

  • 您是否在 routes.rb 文件中声明了与 ReportsController#showEntries 匹配的路由?
  • 啊,是的,我做到了。我刚刚也用我的 routes.rb 文件编辑了这篇文章。
  • 你可以试试这个:&lt;%= button_to "Select Date", {:action =&gt; 'showEntries'}, :remote =&gt; 'true' %&gt;好吗?这很奇怪,就像您的按钮试图在 url 上添加 params[:remote] 而不是将其放入 HTML ...您可以发布 button_to 的输出吗?
  • 我的 button_to 的输出是 &lt;form class="button_to" method="post" data-remote="true" action="/reports/showEntries"&gt; &lt;div&gt; &lt;input type="submit" value="Select Date"&gt; &lt;input type="hidden" value="SiJ725k8z2bIDkg1Z8fe62lNXkOW3mpp/UeLLdb7iPY=" name="authenticity_token"&gt; &lt;/div&gt; &lt;/form&gt; 你发布的内容似乎解决了路由问题,但是当我单击按钮时它似乎仍然没有刷新 div。

标签: jquery ruby-on-rails ajax


【解决方案1】:

您应该将动作 showEntries 的名称更改为 show_entries。我猜你的控制器中有一个动作 show_entries 并且在路由中拼错了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多