如何将变量从AJAX表单传递到控制器? [英] How to pass variables from AJAX form into controller?

查看:88
本文介绍了如何将变量从AJAX表单传递到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格上有报告(金额,日期).我希望用户使用日期选择器选择日期并显示在用户选择的日期之间创建的报告.

所以我需要从AJAX形式传递这些变量.

我添加了AJAX表单.这是我的 _form.html.erb :

I have table on with reports(amount, date). I want user to choose date using datepickers and show reports, that are created between dates user's chose.

So I need to pass these variables from AJAX form.

I added AJAX form. Here is my _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

And I have partial, which is showing report. |

_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 中,我要返回适当的报告:

In my report.js.erb I want to return apporpiate reports:

         $('.report').apppend('<%= escape_javascript(render(@financial_report)) %>');

这是我的控制器(我不知道应该在哪里称哪里"条件

       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:

Here are my HTML of report form:

     <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获取变量:

I can get variables using 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的东西?

P.S.当我打电话给举报操作时,它显示空白页面.

How should I solve this problem ? Maybe, I missunderstand AJAX stuff ?

P.S. When I'm calling report action it shows me blank page.

推荐答案

您说您能够从ajax获得响应. 但是,您从动作中返回了什么吗? 如果在触发时Ajax请求显式工作正常,则意味着您的路由是正确的.但是ajax请求应该触发您在report.js.erb中拥有的javascript. 如果那没发生,我怀疑参数的格式错误.我可以看到在ajax请求中您正在传递:

You say you are able to get a response from the ajax . But are you returning anything from the action ? If the ajax request when fired explicitly works fine , that means your routes are fine . But the ajax request should trigger the javascript you have in report.js.erb . If that doesn't happen , I suspect parameters to be in the wrong format . I can see that in the ajax request you are passing :

  data: {'start_date' : $("input[name='report[start_date]']").val(), 
    'end_date' : $("input[name='report[end_date]']").val() } 

如果动作需要参数params [:start_date]和params [:end_date],则很好.但是该表单不会以该格式发送参数.

This is fine if the action is expecting params[:start_date] and params[:end_date] . But the form does not send the params in that format .

我的建议: 使用firebug或其他调试工具,确保它是POST请求,并且参数应采用预期的格式.检查请求的响应以查看返回值.这应该是成功"的回应.还要检查控制器中的报告"方法是否为公共方法.

My suggestion : With firebug or some other debugging tool , ensure it's a POST request , and the parameters should be in the expected format . Inspect the response of the request to see the return value . It should be a "success" response . Also check that the method "report" in the controller is a public method .

这篇关于如何将变量从AJAX表单传递到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆