我有报告表(金额,日期)。我希望用户使用日期选择器选择日期并显示在用户选择的日期之间创建的报告。
所以我需要从 AJAX 表单中传递这些变量。
我添加了 AJAX 表单。这是我的_form.html.erb:
<%= form_tag(:controller => "financial_reports", :action => 'report', :remote => true) do%>
  <span id="pickers">From</span> 
  <%= datepicker_input "report","start_date", :dateFormat => "dd/mm/yy" %>
  <span id="pickers2">To</span>
  <%= datepicker_input "report", "end_date", :dateFormat => "dd/mm/yy"%>
<%=  submit_tag "Run Report", :class => "btn super", :id => "btn "%>
我有部分,正在显示报告。|
_financial_report.erb.html
 <table class="table" id="performance_report">
  <thead>
  <th>Date</th>
<th>Amount</th>
<th>Currency</th>
  </thead>
 <% @financial_reports.each do |financial_report| %>
  <tr>
   <td> <%= financial_report.created_at.strftime("%d/%m/%Y")%></td> </td>
<td><%= financial_report.amount %></td>
<td><%= financial_report.currency %></td>
  </tr>
  <% end %>
    <tr>
  <td>Total</td>
  <td><%= current_user.financial_reports.sum(:amount)%></td>
  </tr>
     </table>
我正在我的index.erb.html上渲染这个部分
            <div id="report_form"><%= render 'form' %></div>
             <%= render 'financial_report' %>
在我的report.js.erb中,我想返回适当的报告:
         $('.report').apppend('<%= escape_javascript(render(@financial_report)) %>');
这是我的控制器(我不知道我应该在哪里调用“where”条件
       def index
          @financial_reports = current_user.financial_reports#.where(:created_at => the_start.to_date..the_end.to_date)
       ...
     end
或在这里
  def report
    @financial_reports = current_user.financial_reports#.where(:created_at => the_start.to_date..the_end.to_date)//here
     respond_to do |format|
    format.js
   end
  end
这是我的报告表单的 HTML:
     <form accept-charset="UTF-8" action="/financial_reports/report?remote=true" method="post">
     <input id="report_start_date" name="report[start_date]" size="30" type="text" class="hasDatepicker">
      <input id="report_end_date" name="report[end_date]" size="30" type="text" class="hasDatepicker">
在我的路线中:
        match 'financial_reports/report' => 'financial_reports#report'
我可以使用 AJAX 获取变量:
  $.ajax({ 
  type: 'POST', 
  url: 'financial_reports/report', 
  data: {'start_date' : $("input[name='report[start_date]']").val(), 
    'end_date' : $("input[name='report[end_date]']").val() }, 
  success: function(data){
 //data is whatever you RETURN from your controller. 
    } 
 }); 
我应该如何解决这个问题?也许,我误解了 AJAX 的东西?
PS 当我调用报告操作时,它会显示空白页。