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 Building Social Features in Ruby on Rails Adding State Test Factories

Treebook, not able to set @friend variable for user_friendships

When i got to /user_friendships in my browser i get this error: No route matches {:action=>"show", :controller=>"users", :id=>nil} missing required keys: [:id].

However all my tests are passing. It just appears that the friend variable isnt getting passed an ID. Heres my code for user_friendships/index.html.erb:

    <h1>Friends</h1>
</div>

<% @user_friendship.each do |friendship| %>
    <% friend = friendship.friend %>
    <div id="<%= dom_id(friendship) %>" class="friend">
      <div class="">
        <%= link_to 'friend.full_name', user_path(friend) %>
      </div>
      <div class="">
        <strong><%= friend.full_name %></strong><br />

          <% if friendship.pending? %>
            <em>Friendship is pending.</em> <%= link_to "Delete request", edit_user_friendship_path(friendship.friend.profile_name) %>.
          <% end %>

          <% if friendship.requested? %>
            <em>Friendship requested.</em> <%= link_to "Accept friendship", edit_user_friendship_path(friendship.friend.profile_name) %>.
          <% end %>

          <% if friendship.accepted? %>
            <em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship.friend.profile_name) %>
          <% end %>
      </div>
    </div>
<% end %>''' 

You'll see that I get an error on this line: ```  <%= link_to 'friend.full_name', user_path(friend) %>```. user_path show is where you view a users profile info as I created an users_controller with different methods to update an aboutme and gender attributes and the friend variable isn't passing the ID to be able to view the profile or get its full_name method.

Here's my code for user_friendships_controller: 
```class UserFriendshipsController < ApplicationController
    before_filter :authenticate_user!

    def new 
        if params[:friend_id]
            @friend = User.where(profile_name: params[:friend_id]).first
            raise ActiveRecord::RecordNotFound if @friend.nil?
            @user_friendship = current_user.user_friendships.new(friend: @friend)
        else
            flash[:error] = "Friend required"
        end
    rescue ActiveRecord::RecordNotFound
        render file: 'public/404', status: :not_found
    end

    def create
        if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
            @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
            @user_friendship = current_user.user_friendships.new(friend: @friend)
            @user_friendship.save
            flash[:success] = "Network invitation sent to @friend.first_name."
            redirect_to user_path(@friend)
        else
            flash[:error] = "Friend required"
            redirect_to root_path
        end 
    end

    def index
        @user_friendship = current_user.user_friendships.all
    end

    def accept
        @user_friendship = current_user.user_friendships.find(params[:id])
        if @user_friendship.accept!
            flash[:success] = "You are now friends with #{@user_friendship.friend.full_name}"
        else
            flash[:error] = "That friendship could not be accepted."
        end 
        redirect_to user_friendships_path
    end

    def edit
        @user_friendship = current_user.user_friendships.find(params[:id])
        @friend = @user_friendship.friend
    end
end'''

Also when I request a friend the message says "Friend @friend.full_name requested"

If you need anything else just let me know. and thanks in advance!

@[Jason Seifer](https://teamtreehouse.com/jasonseifer)

1 Answer

Benito Miranda
Benito Miranda
310 Points

Hi, I haven't begin working in this application, but based in my experience, here's what is going on.

As you can see in the following error message:

No route matches {:action=>"show", :controller=>"users", :id=>nil} missing required keys: [:id].

You're passing a null parameter for id.

What you need to do is fix the parameter you are passing to the user_path in the code below, it should be something like friend.id

<%= link_to 'friend.full_name', user_path(ADD THE FRIEND ID HERE) %>

I hope this helps, Cheers!

Ben