Biju
Biju

Reputation: 978

Read Rails form data inside a javascript code before doing a form submit

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?

Upvotes: 1

Views: 788

Answers (1)

kishore cheruku
kishore cheruku

Reputation: 521

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

Upvotes: 2

Related Questions