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

Ruby

Andrew Merrick
Andrew Merrick
20,151 Points

[SOLVED] Implementing AJAX - Part 2 - Add Friend jquery button not working...

After completing videos 1 and 2 of the Implementing AJAX portion, my Add Friend button does not work. I opened the Google Chrome Developer Tools and I observed:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

My server logs show

NoMethodError (undefined method json' for #<UserFriendship:0x007f94bbfe7de0>): app/controllers/user_friendships_controller.rb:62:inblock (2 levels) in create' app/controllers/user_friendships_controller.rb:50:in `create'

Rendered /usr/local/rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms) Rendered /usr/local/rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms) Rendered /usr/local/rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (7.8ms)

When I then click on Friends in the browser it says that the friendship request was sent. So, the request worked but the button does not.

Any ideas?

1 Answer

Andrew Merrick
Andrew Merrick
20,151 Points

After checking:

app/controllers/user_friendships_controller.rb:62:inblock (2 levels) in create' app/controllers/user_friendships_controller.rb:50:in `create'

I found that in lines 62 of my user_friendships_controller.rb I had .json instead of .to_json

Should be this:

format.json { render json: @user_friendship.to_json, status: :precondition_failed }

but I had this:

format.json { render json: @user_friendship.json, status: :precondition_failed }