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

JavaScript

[Solved] Implementing AJAX Part3 - Error when click 'Add Friend' button, javascript not working.

(https://teamtreehouse.com/library/programming/building-social-features-in-ruby-on-rails/using-ajax/implementing-ajax-part-3)

I followed the video but when I click 'Add Friend' button, it shows following error message on Chrome console, and the button does not change to 'Friend Requested'.

POST http://localhost:3000/user_friendships?user_friendship%5Bfriend_id%5D= 500 (Internal Server Error) 

My code: (user_friendships.js) (https://github.com/yhagio/pwitter/blob/master/app/assets/javascripts/user_friendships.js)

$(document).ready(function(){

    $('#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>");
            }
        });
    });

});

My code: (show.html.erb) (https://github.com/yhagio/pwitter/blob/master/app/views/profiles/show.html.erb)

<% if signed_in? %>
<div id="friend-status">
     <% if current_user.friends.include?(@user) || current_user.pending_friends.include?(@user) %>
        <%= link_to "Edit Friendship", edit_user_friendship_path(friend_id: @user), class: 'btn' %>
    <% else %>
        <%= link_to "Add Friend", new_user_friendship_path(friend_id: @user), class: 'btn', id: 'add-friendship', data: { friend_id: @user.to_param } %>
    <% end %>
</div>
<% end %>

9 Answers

I'm not sure what question you're asking.

The link you provided points your own machine (localhost) and even though most servers can be setup to handle such a request, I don't see an actual page that's being requested.

It starts off in the "user_friendships" node and then has a ? in it (which primarily denotes the beginning of the querystring) so everything after that point would be a variable or value that the called page processes.

is user_firendships a page? For me if it was a ColdFusion page, the link would look like http://localhost:3000/user_friendships.cfm?querystring here

Just a note, %5B and %5D are [ and ] respectively. What are you trying to do?

Jason, Yes, that's the error message I got in the console on Chrome when I clicked the button "Add Friend".

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Sorry, Yuichi, that was a typo. What is the exact error you're getting?

This is the error message I got in the console on Chrome when I clicked the button "Add Friend" and nothing happens after I click, so I cannot add friend:

POST http://localhost:3000/user_friendships?user_friendship%5Bfriend_id%5D= 500 (Internal Server Error)

Screenshot: https://docs.google.com/file/d/0B7APJiFp_MS4VVNFZUtneWlKOEk/edit?usp=sharing

I still cannot solve this problem on my own, maybe I should start from zero again. But, I still try to figure it out.

Are you using Firefox as your browser? If so, does this error occur if you use Chrome?

Problem solved, it was just a minor mistake, it was the case problem of "id". Works on any browser :)

Yuichi can you explain in more detail? I am running into the same issue and having trouble figuring it out as well. Thank you!

EDIT: Never mind, I figured it out with the help of this stackoverflow post: http://stackoverflow.com/questions/15229038/rails-and-html-data-attributes-use-dash-or-underscore Basically, I changed 'friendID' to 'friend-id'