您好,我是Rails上的红宝石新手。我正在尝试构建一个具有日期字段的页面,并且在选择了特定日期之后,我想生成一个显示该日期选择报告的表。
controllers / reports_controller.rb
class ReportsController < ApplicationController
def index
end
def test
@chosen_date = params[:report_date]
@incoming_messages = Message.where("inbound = 't' and created_at like '" + @chosen_date + "%'").count
@total_chats = Message.where("created_at like '" + @chosen_date + "%'").distinct.count(:phone_number)
@enrollment_by_andi = Enrollment.where("created_at like '" + @chosen_date + "%' and created_by = 'ANDI'").count
@enrollment_by_agent = Enrollment.where("created_at like '" + @chosen_date + "%' and created_by <> 'ANDI'").count
@sent_by_andi = Message.where("created_by = 'ANDI' and created_at like '" + @chosen_date + "%'").count
@sent_by_agents = Message.where("inbound = 'f' and created_by <> 'ANDI' and created_at like '" + @chosen_date + "%'").count
@unread_messages = Message.where("created_at like '" + @chosen_date + "%' and is_read = 'f'").distinct.count(:phone_number)
end
end
views / reports / index.html.erb
<h1>Reports</h1>
<%= form_tag(reports_test_path,:method => "post") do %>
<%= label_tag(:report_date,"Choose a Date") %>
<%= date_field_tag(:report_date)%>
<%= button_tag "Submit"%>
<% end %>
我要生成的表:
<table>
<th>
<td>Incoming Messages</td>
<td>Total Chats</td>
<td>Enrollment By Andi</td>
<td>Enrollment By Agent</td>
<td>Sent By Andi</td>
<td>Sent By Agents</td>
<td>Unread Messages</td>
</th>
<tr>
<td><%= @incoming_messages %></td>
<td><%= @total_chats %></td>
<td><%= @enrollment_by_andi %></td>
<td><%= @enrollment_by_agent %></td>
<td><%= @sent_by_andi %></td>
<td><%= @sent_by_agents %></td>
<td><%= @unread_messages %></td>
</tr>
</table>
下面是页面的图像:
任何人都可以帮助我找到一种在按下按钮时在日期字段下生成表格的方法?
您可以通过在表单标签上设置remote: true
通过Ajax提交表单:
<%= form_tag(reports_test_path,:method => "post", remote: true) do %>
<%= label_tag(:report_date,"Choose a Date") %>
<%= date_field_tag(:report_date)%>
<%= button_tag "Submit"%>
<% end %>
然后您将拥有一个相应的`app / views / reports / test.js.erb视图文件,该文件会生成将在客户端发送和执行的实际JavaScript代码。
您可以将表格放在不完整的地方,例如reports/_table.html.erb
并在javascript中呈现:
var tableContainer = document.getElementById(...);
tableContainer.insertAdjacentHTML('beforeend', '<%= j render partial: "/reports/table" %>');
如果您对使用Ajax和javascript不太熟悉,请查看Ruby on Rails guides