您好,我是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>
下面是页面的图像:
当我按下按钮时,任何人都可以帮助我找到一种在日期字段下生成表格的方法吗?
答案 0 :(得分:1)
您可以通过在表单标签上设置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代码,这些代码将在客户端发送和执行。
您可以将表格放在部分位置,例如app/views/reports/_table.html.erb
并将其呈现在javascript中:
var tableContainer = document.getElementById(...);
tableContainer.insertAdjacentHTML('beforeend', '<%= j render partial: "/reports/table" %>');
如果您对使用Ajax和javascript不太熟悉,请查看Ruby on Rails guides
答案 1 :(得分:1)
您可以通过服务器端渲染来实现
添加具有以下内容的模板文件:
views / reports / test.html.erb
<h1>Reports</h1>
<%# The date field from the index.html %>
<%= form_tag(reports_test_path,:method => "post") do %>
<%= label_tag(:report_date,"Choose a Date") %>
<%= date_field_tag(:report_date)%>
<%= button_tag "Submit"%>
<% end %>
<%# the new content %>
<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>
您可以稍后使用局部词等减少重复,但这并不重要。
更重要的问题是您的查询至少看起来可以使用它们进行SQL注入(有关“ SQL注入活动记录轨”的博客文章,Google)。熟悉一下ActiveRecord指南,这样做会更安全:
@chosen_date = Date.parse params[:report_date]
@incoming_messages = Message.where(inbound: 't').where(created_at: @chosen_date).count
此外,您不需要自己编写SQL。
束缚.where()
导致SQL产生AND
,并将Date和Time对象(和范围)传递到where()
方法中,这是对Rails以后几年的改进。
答案 2 :(得分:1)
您可以使用AJAX,它非常适合导轨并且非常不错。为此,请执行以下步骤-
_report_table.html.erb
。桌子上有个小问题。因此,将下表html粘贴到部分-<table>
<tr>
<th>Incoming Messages</th>
<th>Total Chats</th>
<th>Enrollment By Andi</th>
<th>Enrollment By Agent</th>
<th>Sent By Andi</th>
<th>Sent By Agents</th>
<th>Unread Messages</th>
</tr>
<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>
test.js.erb
创建一个js文件,因为您的发布方法名称为test,它将html表放入index.html.erb
中的div中$('#report_table').html("<%= escape_javascript(render partial: 'report_table') %>");
<div id="report_table"></div>
form_tag(reports_test_path,:method => "post",:remote=>true)
respond_to do |format|
format.js
end
应该这样做。如果您有任何困惑,请告诉我