使用AJAX和Rails刷新部分时出现问题



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

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

到目前为止,我拥有的是:

在/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调用,那就太好了。谢谢<3

您应该将操作showEntries的名称更改为show_entries。我猜你的控制器里有一个动作秀条目,在路线上拼错了。

最新更新