Short story:
I am trying to pass a variable from JavaScript (specifically, jQuery) as a parameter in an embedded ruby method.
Long story:
It all begins with a function. In summary, I have date picker built via jQuery UI.
#my_budget.html.erb
<h1 class="text-center">Your Budget</h1>
<h4 class="text-center">Select Month to View</h4>
<input style="width:200px" name="startDate" id="startDate" class="date-picker form-control" placeholder="Select Month"/>
<br>
<br>
<script type="text/javascript">
function setDateRange() {
$('.date-picker').change(function() {
var date = $(this).val();
return date
});
}
$(document).ready(setDateRange);
</script>
Further down the road, I have embedded ruby attempting to make a call to said variable (date). This value is passed via MM/YYYY format.
#my_budget.html.erb
<td class="section-value"><%= number_to_currency(@user.total_income(date)) %></td>
Here's a peek at my model.
#user.rb
# General Income
def total_income(current_month)
total_value = Transaction.joins(:user).where("transactions.sub_category" => '11').where("created_at = ?", current_month).sum(:amount)
total_value
end
For fairly obvious reasons, this does not work.
I am a junior developer in Rails and am still learning when to use Ruby versus JS for certain functions.
What is the best way to go about passing my JS variable into my embedded Ruby? Alternatively, how can I call the input value "on change" via Ruby into my function, so when a user selects "March 2017" in the date picker, Ruby says "Hey, it changed! Let's use it!" and adjusts the value accordingly.
Thank you!