如何将数据从jQuery传递到控制器? [英] How to pass data from jQuery to controller?

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

问题描述

我有两个字段 - 结束和开始日期(daterange),我应该将它们传递到控制器并重新加载此页面。

这是我的代码视图:

 <%= form_tag(:controller => (例如,financial_reports,:action =>'index',:method =>'post')do%> //也许这行应该被编辑
<%= datepicker_inputreport,start_date :dateFormat => dd / mm / yy%>
<%= datepicker_inputreport,end_date,:dateFormat => dd / mm / yy%>
<%end%>

和生成的HTML():

 < form accept-charset =UTF-8action =/ financial_reports?method = postmethod =post...> 
< input id =report_start_datename =report [start_date]size =30type =textclass =hasDatepicker>< script type =text / javascript
< input id =report_end_datename =report [end_date]size =30type =textclass =hasDatepicker
< input class =btn wideid = btn-reportname =committype =submitvalue =Run Report>
< / form>

我可以像这样获得datepicker值:

  $('#datepicker')。datepicker({
onSelect:function(dateText,inst){
$(input [name ='report [start_date ]'])。val(dateText);
}
});

我需要将它们传递到控制器并重新加载此页面,因为在此页面控制器。

financial_reports.where(:created_at => start_date.to_date..end_date.to_date)

添加其他操作或方法?你可以建议什么?

解决方案

在我看来,使用AJAX而不是重新加载页面是最好的选择。您只是尝试更新一些数据,没有理由执行完整的页面刷新。我不是一个RoR的爱好者,但似乎我们可以发送数据到控制器通过执行 - >

  $ .ajax({
type:'POST',
url:'path / to / controller',
data:{'start_date':$(input [name ='report [start_date ]'])。val(),
'end_date':$(input [name ='report [end_date]']) {
//数据是从控制器返回的任何数据
//数组,字符串,对象...某些
}
});`

在您的RoR控制器中,您将通过执行...获取参数。

  var the_start = params [:start_date],
the_end = params [:end_date]

现在你已经得到了控制器中的数据,你可以应用该方法。然后,当您准备好发送数据时,返回,打印,无论您向页面发送数据。



此外,RoR默认情况下(当访问控制器)期望输出一个模板到视图,因为我们只是想更新数据,你需要停止该默认动作使用 - >

  render:layout => false 

我希望这是有帮助的。我真的不是RoR的专家,但这是我在几次尝试中使用的方法。



Goodluck!


I have two fields - end and start date(daterange) and I should pass them into controller and reload this page.

Here is my code from view:

    <%= form_tag(:controller => "financial_reports", :action => 'index', :method => 'post') do%>//maybe this line should be edited
   <%= datepicker_input "report","start_date", :dateFormat => "dd/mm/yy" %>
  <%= datepicker_input "report", "end_date", :dateFormat => "dd/mm/yy"%>
  <%end%>

and generated HTML() :

  <form accept-charset="UTF-8" action="/financial_reports?method=post" method="post"...>
  <input id="report_start_date" name="report[start_date]" size="30" type="text" class="hasDatepicker"><script type="text/javascript">
<input id="report_end_date" name="report[end_date]" size="30" type="text" class="hasDatepicker"
    <input class="btn wide" id="btn-report" name="commit" type="submit" value="Run Report">
  </form>

Can I get datepicker value like this :

      $('#datepicker').datepicker({
    onSelect: function(dateText, inst) {
  $("input[name='report[start_date]']").val(dateText);
    }
});

and I need to pass them into controller and reload this page, because in this page controller.

HERE I NEED TO PASS JQUERY VALUE

    @financial_reports = current_user.financial_reports.where(:created_at => start_date.to_date..end_date.to_date)

or I should add another action or method ? What you can suggest ?

解决方案

In my opinion, using AJAX and not reloading the page is a best bet here. You're simply trying to update some data, and there's no reason to perform a full page-refresh to do that. I'm not a RoR aficionado, but it would seem that we can send the data to the controller by doing ->

$.ajax({ 
  type: 'POST', 
  url: 'path/to/controller', 
  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. 
    //an array, string, object...something 
  } 
});`  

In your RoR controller, you'll get the params by doing...

var the_start = params[:start_date],
    the_end = params[:end_date]

Now you've got the data in your controller, you can apply the method to it. Then when you're ready to send the data back, return, print, whatever you do to send data to the page.

Also, RoR by default (when accessing a controller) expects to output a template to the view, since we just want to update data, you'll need to stop that default action using something like ->

render :layout => false

I hope this was helpful. I'm really not a RoR expert, but this is the method I used in the few times I dabbled.

Goodluck!

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

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