blc
blc

Reputation: 161

Can't get Rails return value in jQuery

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 " ".

Upvotes: 1

Views: 706

Answers (1)

x1a4
x1a4

Reputation: 19496

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

render :text => 'success'

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.

Upvotes: 2

Related Questions