Skip to content Skip to sidebar Skip to footer

Jquery Call To Controller Action Is Not As Expected

I have the following jQuery: var request = $.get('getPerforceSuites'); request.success(function(result){ alert('res: ' + result) }) request.error(function(jqXHR, textStatus, er

Solution 1:

This is due to the way rails responds to controller actions. By default, if you don't call render in the controller action, then it will look in your views folder for a template. That's why it wanted you to create a getPerforce.erb.html file.

To make it just return plain text, in the controller you can write:

render :text =>'Hi!'

If you are doing AJAX stuff, then typically you will want to return JSON, you can also do this in the controller like so

render :json => {:data_key => 'A value'}

Solution 2:

I would suggest the reason why the getPerforce.js.erb is outputting an <html> header is because the server is already outputting content with your puts commands


HTML

Although the official terminology escapes me, you must remember Rails is a framework based on HTTP -- meaning any outputted has to be transmitted to the client

As such, if you're outputting text, it's got to be rendered, which I would guess is done with the Rails HTML rendering engine


Controller

As alluded to by Slicedpan, I would get rid of any direct outputs (puts) from the controller.

You should use this:

#app/controllers/perforce_sync_controller.rb
def getPerforce
   respond_to do |format|
       format.html { @return = "Called getPerforce" }
       format.js { @return = "hi" }
   endend

#app/views/perforce_sync/getPerforce.js.erb
alert("<%=j @return %>");

Post a Comment for "Jquery Call To Controller Action Is Not As Expected"