【问题标题】:Is there a method to show a table after pressing on the button?按下按钮后是否有显示表格的方法?
【发布时间】:2020-04-12 05:30:48
【问题描述】:

您好,我是 ruby​​ on rails 的新手。我正在尝试构建一个包含日期字段的页面,在选择特定日期后,我想生成一个显示此日期选择报告的表格。

控制器/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

视图/报告/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>

以下是页面图片:

当我按下按钮时,任何人都可以帮助我找到在日期字段下生成表格的方法吗?

【问题讨论】:

    标签: html ruby-on-rails model-view-controller ruby-on-rails-5


    【解决方案1】:

    您可以使用 AJAX,它非常适合 rails 并且非常不错。为此,请执行以下步骤 -

    1. 首先创建一个部分_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>
    
    1. 创建一个 js 文件为 test.js.erb,因为你的 post 方法名称是 test,它将表格 html 放在 index.html.erb 的一个 div 中
    $('#report_table').html("<%= escape_javascript(render partial: 'report_table') %>");
    
    1. 在表单后的 index.html.erb 中添加一个带有 report_table id 的 div

    &lt;div id="report_table"&gt;&lt;/div&gt;

    1. 在表单中添加remote true

    form_tag(reports_test_path,:method =&gt; "post",:remote=&gt;true)

    1. 在渲染所需js文件的测试方法中添加return
    respond_to do |format|
      format.js
    end
    

    应该这样做。如果您有任何困惑,请告诉我

    【讨论】:

    • 谢谢你,我按照你说的做了,效果很好
    【解决方案2】:

    你可以通过服务器端渲染来做到这一点

    添加一个模板文件,内容如下:

    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 注入活动记录轨道”的博客文章)。多熟悉一下 ActiveRecord 指南,这样做会更安全:

    @chosen_date = Date.parse params[:report_date]
    
    @incoming_messages = Message.where(inbound: 't').where(created_at: @chosen_date).count
    
    

    另外,您不需要自己编写 SQL。

    在 SQL 中将 .where() 链接到 AND 并将日期和时间对象(和范围)传递给 where() 方法是后来 Rails 版本中的改进

    【讨论】:

    • 感谢您告诉我有关 SQL 注入的信息。
    【解决方案3】:

    您可以通过在表单标签上设置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

    【讨论】:

    • 我认为问题不在于 AJAX 请求和将动态内容添加到文档中。对于这种情况,服务器端渲染完全没问题
    猜你喜欢
    • 2014-04-24
    • 2013-08-13
    • 1970-01-01
    • 2017-08-27
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多