Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andy Brown
6,310 PointsAJAX 'GET' request getting error. (RoR Stage 4, Video 5)
I am stuck on the very last portion of the 'Using Ajax' videos.
My problem is that when trying to load an array of user friendships, the get request in the AJAX function is failing. I'm not sure if it is an issue with the url or if the data coming back is not formatted as json.
Here is the code:
window.userFriendships = [];
$(document).ready(function(){
$.ajax({
type: 'GET',
url: Routes.user_friendships_path({format: 'json'}),
dataType: 'json',
success: function(data) {
window.userFriendships = data;
console.log('data added');
},
error: function(data) {
console.log("data get failed");
}
});
$('#add-friendship').click(function(event){
event.preventDefault();
var addFriendshipBtn = $(this);
$.ajax({
url: Routes.user_friendships_path({user_friendship: {friend_id: addFriendshipBtn.data('friendId')}}),
dataType: 'json',
type: 'POST',
success: function(e){
addFriendshipBtn.hide();
$('#friend-status').html("<a href='#' class='btn btn-success'>Friendship Requested</a>");
}
});
});
});
6 Answers
Jason Seifer
Treehouse Guest TeacherHey Andy, can you paste the error you're getting in the browser?
Andy Brown
6,310 PointsI don't see a specific error, only the error message I have set in the AJAX call.
Here is a screenshot of the "Network" tab on the Chrome console. I don't see anything in there about the data get failing, but the data is definitely not getting added to the array.
Jason Seifer
Treehouse Guest TeacherTry clicking on the "Console" tab and paste a screenshot after typing in "window.userFriendships".
Andy Brown
6,310 PointsIt is just a blank array.
Andy Brown
6,310 PointsJason,
I have figured out the problem. I am using the Ruby Mine IDE and when I created the views for the user_friendship controller, they were created as "index.erb" etc. instead of "index.html.erb". I'm not sure why I needed to make that change, but it seemed to correct the issue!
Thanks for looking into it for me.
Jason Seifer
Treehouse Guest TeacherThat would do it, thanks for the update, Andy!

