I am trying to understand Rails built in AJAX helper -> remote: true
.
I have a link in a view (views/show
) that calls a controller via AJAX:
<%= link_to 'My Profile', edit_user_path(current_user), class: 'user-profile', remote: true %>
Which hits the appropriate controller (controllers/users_controller.rb
) with the appropriate fields (tested with byebug):
def edit
@user = User.find(params[:id])
respond_to do |format|
format.html { render plain: 'profile' }
format.js { render plain: 'profile' }
format.json { render plain: 'test' }
end
end
And I have my JS (javascripts/users.js
) perform an action on ajax success or failure:
$(document).ready(function() {
$('.user-profile').on('ajax:success', function(event) { console.log(event) });
$('.user-profile').on('ajax:error', function(event) { console.log(event) });
});
However, no matter what I try, the ajax:error
function always executes and I cannot figure out why. Any ideas?