3

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 ?

4
  • What piece of the puzzle are you missing here? Are you simply looking for a way to pass the data to the controller before the form gets submitted? Commented Aug 6, 2012 at 14:35
  • Yes. Should I reload page to pass data to controller ? Commented Aug 6, 2012 at 14:36
  • you can pass the data to the controller using AJAX so that they never have to leave the page. Or, you could pass the data using AJAX, then reload the page in the success function if you're trying to update some data on the page. It's up to you, really. Do you know how to pass data to a controller using AJAX? Commented Aug 6, 2012 at 14:39
  • No, I'm not familar with it. Maybe I can just reload page and pass data ? What would be better ? Commented Aug 6, 2012 at 14:41

2 Answers 2

5

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.