Parser error when using jQuery-UJS for remote link_to in Rails 3 app: how can I debug this?

后端 未结 7 1980
刺人心
刺人心 2021-02-14 12:55

I\'m trying to replace the content of a div after clicking on a link using Rails 3, remote_link :remote => true and jQuery.

So far, I\'ve been able to ge

7条回答
  •  抹茶落季
    2021-02-14 13:33

    I'm going to propose an answer because comments don't allow for any formatting. Here is is: Something is happening on the server side and jQuery is not getting what you think it is. Here's an excerpt from the jQuery documentation:

    error(jqXHR, textStatus, errorThrown)Function A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

    That implies that your controller may be responding with something other than the expected data. In that controller, try:

    Rails.logger.debug render_to_string(:partial => "followings/follow")
    

    In any case, check your logs to make sure what you think is happening really is happening. Also, write a test to verify this:

    # controller spec... modify if using Test::Unit
    it "sends cool javascript" do
      xhr.post :unfollow, :id => 83, :data-method => "delete"
      response.body should == "some known response"
    end
    

    Ok, it's a hacky, brittle spec, but it will do until you know where things are going wrong.

    Once you get this working, everything else will fall neatly into place.

提交回复
热议问题