I'm trying to call a javascript function from an html.erb file. The javascript function makes an Ajax call to a specified controller, sending an item id from the URL as a parameter.
I keep getting syntax and "can't find variable: callAjax" errors.
in the html.erb file
<%= image_tag('star.png', :style => 'margin-right: 10px', :onclick => "callAjax('/book/favorite/'+book.id);") %>
in the js file
function callAjax(path) {
console.log(path);
$.ajax({
url: 'localhost:3000'+path,
data: path.substring(path.lastIndexOf('/') + 1),
success:{
//do something
}
});
}
in the book controller
def favorite
book=Book.find(params[:id])
current_user.favorite_books << book
end