I have a Rails method that just returns a string, and for some reason, I can’t seem to get the success return value in the JavaScript.
def count
render :nothing => true
return "success"
end
$.post("/home/count",
function(data) {
document.getElementById("test_call_button").value = 'Calling...' + count;
});
The object that is returned by $.post("/home/count"); has a responseText of " ".
The return value of a controller action is not sent back to the requester; what you render is, and in this case you’re specifically rendering
:nothing.If you want to see the result in your javascript, change your render line to
I would urge you to render a json object as your response though, which makes it trivial to add other fields in the future should they be necessary.
Also make sure to check out the Rails rendering guide.