Reputation: 48521
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
Upvotes: 0
Views: 89
Reputation:
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.
Upvotes: 2
Reputation: 50807
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.
Upvotes: 0