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

RoR - Adding State: Accepting Friendships

Stuck... followed Jason's code to the T. I get the following error when I try to accept a friendship or delete a pending request.

ActiveRecord::RecordNotFound in UserFriendshipsController#edit 
 Couldn't find UserFriendship without an ID

app/controllers/user_friendships_controller.rb:54:in `edit'

Request

Parameters:   {"format"=>"12"}

Here's my edit method (line 54 in my user_friendships_controller.rb)

    def edit 

        @user_friendship = current_user.user_friendships.find(params[:id])
        @friend = @user_friendship.friend

end

Also there's a difference in my url compared to Jason's

Jason's url:

http://localhost:3000/user_friendships/6/edit

My url:

http://localhost:3000/user_friendships/edit.12

All of my tests pass so not sure how to troubleshoot this. Any insight appreciated.

9 Answers

Also I added logger.info "Error HERE!!!!!!!!!!!!!!!!!!!" to help locate in the rails log

Started GET "/user_friendships/edit.12" for 127.0.0.1 at 2013-01-10 12:35:18 -0700
Processing by UserFriendshipsController#edit as
  ?[1m?[35mUser Load (0.0ms)?[0m  SELECT "users".* FROM "users"   WHERE   "users"."id" = 4 LIMIT 1
Error HERE!!!!!!!!!!!!!!!!!!!
Completed 500 Internal Server Error in 94ms

ActiveRecord::RecordNotFound (Couldn't find UserFriendship without an ID):
  app/controllers/user_friendships_controller.rb:54:in `edit'
Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Matthew, can you paste in the code where you link to the friendship?

I think this is what your looking for:

user_friendships/edit.html.erb

 <div class= "page-header">
<h1>Viewing Friendships</h1>

</div>

<% if @user_friendship.requested? %>
    <h3>Do you really want to be friends with <%= @friend.first_name %>?</h3>
<% end %>

<div class="form_actions">
    <% if @user_friendship.requested? %>
        <%= form_for @user_friendship, url: accept_user_friendship_path(@user_friendship), method: 
            :put do |form| %>
        <%= submit_tag "Accepted Friendship", class: 'btn btn-primary' %>
        <% end %>
    <% end %>

</div>

or here

user_friendships\index.html.erb

<div class= "page-header">
<h1>Friends</h1>
</div>

<% @user_friendships.each do |friendship| %> 
<% friend = friendship.friend %>
<div id="<%= dom_id(friendship) %>" class="friend_row">
        <div class = "span1">
            <%= link_to image_tag(friend.gravatar_url), profile_path(friend) %>
        </div>
        <div class = "span7">
            <strong><%= friend.full_name %></strong><br />
                <% if friendship.pending? %>
                    <em>Friendship is pending</em> <%= link_to "Delete Request", edit_user_friendships_path(friendship) %>
                <% end %>
                <% if friendship.requested? %>
                    <em>Friendship requested</em> <%= link_to "Accept Friendship", edit_user_friendships_path(friendship) %>
                <% end %>
                <% if friendship.accepted? %>
                    <em>Friendship started <%= friendship.updated_at %></em> <%= link_to "Update Friendship", edit_user_friendships_path(friendship) %>
                <% end %>
        </div>
</div>
<% end %>

here's my routes

 user_friendships GET  /user_friendships(.:format)   user_friendships#index
 accept_user_friendships PUT  /user_friendships/accept(.:format)    user_friendships#accept
                     POST   /user_friendships(.:format)        user_friendships#create
        new_user_friendships GET    /user_friendships/new(.:format)        user_friendships#new
       edit_user_friendships GET    /user_friendships/edit(.:format)   user_friendships#edit
                         GET    /user_friendships(.:format)        user_friendships#show
                         PUT    /user_friendships(.:format)        user_friendships#update
                         DELETE /user_friendships(.:format)        user_friendships#destroy
Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

In user_friendships/index.html.erb, change "edit_user_friendships_path" to "edit_user_friendship_path" and you should be good to go.

That caused the following:

undefined method `edit_user_friendship_path' for #<#<Class:0x4a6fd78>:0x4a6dea0>
Extracted source (around line #17):

14:                         <em>Friendship is pending</em> <%= link_to "Delete      Request", edit_user_friendship_path(friendship) %>
15:                     <% end %>
16:                     <% if friendship.requested? %>
17:                         <em>Friendship requested</em> <%= link_to "Accept Friendship", edit_user_friendship_path(friendship) %>
18:                     <% end %>
19:                     <% if friendship.accepted? %> 
20:                         <em>Friendship started <%= friendship.updated_at %  ></em> <%= link_to "Update Friendship", edit_user_friendship_path(friendship) %>
Rails.root: C:/sites/lunker

Application Trace | Framework Trace | Full Trace
app/views/user_friendships/index.html.erb:17:in `block in     _app_views_user_friendships_index_html_erb___324464200_39020544'
app/views/user_friendships/index.html.erb:5:in `each'
app/views/user_friendships/index.html.erb:5:in     `_app_views_user_friendships_index_html_erb___324464200_39020544'

Fixed the problem.

When defining my routes I mistakenly had the following

  resource :user_friendships do 
    member do
      get :index
      put :accept
    end
  end

Note that I am missing the s on resources. This routes user_friendships as a singular resource according to [http://guides.rubyonrails.org/routing.html] Section 2.5. I believe the error arose because no id is passed to the controller when using a singular resource.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Great, glad you fixed it! You are right regarding the error as well.