Ad
Passing JavaScript Value To Ruby In ERB
I want to pass JavaScript value as a parameter to the Ruby Function in ERB.
Code for index.html.erb
function ABC(){
variable = document.getElementById('textfield').value;
}
function XYZ(){
<%= ruby_function(variable) %>
}
I need help inorder to achieve this.
Ad
Answer
The appropriate way of achieving this is using ajax in your script.
For instance you can use coffee script and do the something like this.
ABC = ->
variable = document.getElementById('textfield').value
res = $.ajax(
url: '#{method_controller_path}'
method: 'GET'
data:
param: variable
dataType: 'json'
success: (res) ->
res
)
At the end you will have the res object to work with.
Incase you are not using coffee or external script files. You can do this using javascript in the index.html.erb file.
Here is an example for that.
const ABC = function() {
let res;
const variable = document.getElementById('textfield').value;
return res = $.ajax({
url: '#{method_controller_path}',
method: 'GET',
data: {
param: variable
},
dataType: 'json',
success(res) {
return res;
}
});
};
Hope this helps you out. :)
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad