0

This is my "link":

%span{:id => "car_#{car.id}", :onclick => "add_car(#{car.id}"} Add

that leads on to here:

function add_trending(car_id) {
  jQuery.ajax({
    url: "/cars/add_car",
    type: "GET",
    data: {"car_id" : car_id},
    dataType: "html"
  });
}

in controller:

  def add_car   
    @car = Car.find(params[:car_id])
    ...

    respond_to do |format|
      format.js
    end
  end

and add_car.js.erb:

alert("YES");

But the alert window is not popped up. When I take a look at logs, I see there following:

Rendered cars/add_car.js.erb (0.0ms)

The javascript is just rendered, not executed based on logs. How is that possible?

Thanks

1
  • can you show the concreet response from ajax file ? Commented Dec 9, 2013 at 13:38

2 Answers 2

2

If your ajax request returns javascript you must set the dataType : script or use $.getScript function.

In this case you haven't any success callback function.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you looked at your network traffic client-side? I'm guessing that you're sending this down to the client, but it doesn't know what to do with this result. In fact, if that's your entire handler in jQuery, then it won't do anything with the response. You need a success and/or failure handler in the options object passed to jQuery.ajax, or you need to attach one to the promise it returns. In other words, you're probably getting the data back but not doing anything with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.