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 "%>
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>
and I am rendering this partials on my index.erb.html
<div id="report_form"><%= render 'form' %></div>
<%= render 'financial_report' %>
In my report.js.erb I want to return apporpiate reports:
$('.report').apppend('<%= escape_javascript(render(@financial_report)) %>');
Here are my controllers(I don’t know where I should call “where” condition
def index
@financial_reports = current_user.financial_reports#.where(:created_at => the_start.to_date..the_end.to_date)
...
end
or here
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
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">
and in my routes:
match 'financial_reports/report' => 'financial_reports#report'
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.
}
});
How should I solve this problem ? Maybe, I missunderstand AJAX stuff ?
P.S. When I’m calling report action it shows me blank page.
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 :
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 .
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 .