I have a JavaScript variable:
var selectValUser = $('.select_user :selected').val();
I need to use this variable in erb code on the same page, i.e
<%= get_free_hours_for_user(selectValUser) %>
How can I do it?
I have a JavaScript variable:
var selectValUser = $('.select_user :selected').val();
I need to use this variable in erb code on the same page, i.e
<%= get_free_hours_for_user(selectValUser) %>
How can I do it?
You cannot do it because javascript run on client side while the code in ERB file runs at server side, you can send the value using ajax request, Plus here is an awsome rails cast
Here is an example to send message from javascript to controller; When user click on apply we have an action 'set_id' in controller, it get the power, do the validations etc, the show the message in 'id_message' div in views.
$('#apply').live('click', function(event, data, status, xhr) {
event.preventDefault();
return $.ajax({
url: '/users/registrations/set_id',
type: 'GET',
data: {
code: $('#user_id').val()
},
success: function(data, event, status, xhr) {
$('#id_message').html(response);
return $("#id_message").show();
},
error: function(event, data, status, xhr) {
$('#id_message').html(response);
return $("#id_message").show();
}
});
});
Hope it would answer your question
There is no way to do it directly. Reason is, html runs in your server side whereas javascript runs in your local browser.
Additional discussion can be found here.
How to pass a javascript variable into a erb code in a js view?
And regarding different way to try it out, you can start with this.
http://jing.io/t/pass-javascript-variables-to-rails-controller.html
and