When using AJAX, my code looks like this:
$.ajax({
URL: '/update_profile_picture',
type: 'POST',
data: { username: 'exampleuser' },
success: function(response){
alert(response);
},
})
In my routes file (routes.rb):
post '/update_profile_picture' => 'avatars#update_avatar'
This is the method in the Avatar model (avatar.rb) that retrieves the profile picture based on the username:
self.update_avatar(username)
Avatar.where(username: username).select('avatar').last
end
Inside the AvatarsController:
def update_avatar
@username = params[:username]
@result = Avatar.update_avatar(@username)
end
The question now is how to pass the @result variable back to the AJAX response function to alert the database selection result.