1

I have a date_select in my Rails form:

<%= date_select(:production_month, :date, order: [:month, :year], :start_year => @start_year, :end_year => @end_year) %>

I have a JavaScript function that gets called when the value in the above date_select changes:

$('#production_month').change(function () {alert("Yayyy!!!")});

Can I read the value of the date_select inside an erb code which is inturn inside the JavaScript function above?

4
  • 1
    Try this $(this).val(); Commented Dec 4, 2018 at 12:05
  • Is that a Ruby syntax? Commented Dec 4, 2018 at 12:12
  • No that is JS syntax Commented Dec 4, 2018 at 12:13
  • I able to read the value in JavaScript code. But I need to run a rails query using the values selected in my date_select. And I cannot use JavaScript variables inside my rails query. Is there a way to read the date_select values using Ruby syntax? Commented Dec 4, 2018 at 12:16

1 Answer 1

2

Use below code

$('#production_nmonth').change(function() {
    var date = $(this).val();
    $.ajax({
        type: 'post',
        url: 'controller/action'
        data: {dateValue: date},
    success: function(result){
        $('#fieldID').val(result);
    }
    });      
}

write a action in controller and write a logic you want

def action 
   date = params[:dateValue]
   -- do functionality which you want--
   return data
end

Use byebug for to check data flow

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

12 Comments

As I mentioned earlier, I need to be able to do this in Ruby instead of JavaScript. To be more precise, I need to be able to do this in an erb code inside a JavaScript function
Then you can use model , like before_save (or) you can use helper_method in controller , And you can call helper_method in view. (or) you cab write ajax call for onchange function .
Say I make an ajax call to the controller and update an instance variable in the controller. I need a label in the form UI to reflect the changed value of this instance variable. How can I achieve this please? That's my ultimate goal. (the label will display the number of working days in a month+ year. I need to 1) pass the month and year from date_select to controller 2) and in the controller I need to run a query to fetch the number of working days in that month + year and 3) the label in the form UI needs to display this new calculated value that's available in the controller )
Oh ok , write ajax call for onchange and call controller action and wirte the functionality and return the data. In ajax write success function to receive the data from controller. and take css ID for field you want to assign new value . write like this in ajax success call $('#fieldID").val(data) .
please check above changed code. Let me know if you have any doubt.
|

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.