I am trying to change a rails value with ajax and I am having trouble. Here is what I have so far.
ajax
$(button).click(function(){
$.ajax({
url: '/change_value',
type: 'GET'
})
});
Controller
def home
@value = 0
end
def change_value
@value = 1
end
routes
get '/change_value', to: 'static_pages#change_value'
Thank you for all the help.
You need to create a view to respond in js
format:
change_value.js.erb
var newValue = <%= @value %>;
Now, use it in your script:
$(button).click(function(){
$.ajax({
url: '/change_value',
type: 'GET',
success: function(data) {
console.log(newValue);
}
})
});