How to pass data from jQuery to controller?

自古美人都是妖i 提交于 2019-12-21 05:50:55

问题


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 ?


回答1:


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!




回答2:


Rails 3 now has helpers to do exactly what you want. These are known as remote forms and links.

This blog post does a great job of explaining what they do and how they work.



来源:https://stackoverflow.com/questions/11830352/how-to-pass-data-from-jquery-to-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!