Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Building Social Features in Ruby on Rails Building the Friendship UI Creating the Friendship Form

Blank screen when clicking on Follow button

When I click on the Add Friend button from the users profile I get a blank screen. Please see my code below. i am a beginner so bare with me. Thank you

Controller

 class UserFriendshipsController < ApplicationController
   before_filter :authenticate_user!

   def index
     @user_friendships != @user_friendships
   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.name}"
      else
       flash[:error] = "That friendship could not be accepted"
      redirect_to user_friendships_path
   end

   def new
     if params[:friend_id]
       @friend = User.find(params[:friend_id])
       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.find(params[:user_friendship][:friend_id])
       @user_friendship = current_user.user_friendships.new(friend: @friend)
       if @user_friendship.save
         flash[:success] = "You are now friends!"
       else
         flash[:error] = "There was a problem."
       end
       redirect_to user_path(@friend)
     else
       flash[:error] = "Friend required"
       redirect_to root_path
     end
   end

  def edit
  end

 end
 end

Index

   <% @user_Friendships.each do |friendship| %>
        <% friend = friendship.friend %>
            <div id="<%= dom_id(friendship) %>" class="friend row">
         <div class="span1">
        <center><%= link_to image_tag(user.avatar.url(:thumb)), user %></center>
         </div>
         <div class="span7">
            <strong><%= friend.name %></strong><br />
            <%if friendship.pending? %>
            <em>Frienship is pending.</em> <%=link_to "Delete request", edit_user_friendship_path(friendship) %>.
            <% end %>
            <% if friendship.requested? %>
                <em>Friendship requested.</em> <%= link_to "Accept Friendship", edit_user_friendship_path(friendship) %>.
            <% end %>
            <% if friendship.accepted? %>
                     <em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship) %>.
            <% end %>
             </div>
         </div>
     <% end %>

New

          <% if @friend %>
          <h1><%= @friend.name %></h1>

     <p> Would you like to add <%= @friend.name %>?</p>

     <%= form_for @user_friendships, method: :post do |form| %>
    <div class="form form-actions">
        <%= form.hidden_field :friend_id, value: @friend %>
        <%= submit_tag "Yes, Add Friend", class: 'btn btn-primary' %>
        <%= link_to "Cancel", current_user, class: 'btn btn-default' %>
    </div>  
    <% end %>

    <% end %>

The link for the button is pointing to: <%= link_to "Follow", new_user_friendship_path(friend_id: @user), class: "btn btn-info btn-sm" %>

2 Answers

Gergő Bogdán
Gergő Bogdán
6,664 Points

I am not a ruby expert, but are you sure the correct method is executed when you press the Add Friend button?

Hi Gergo,

I have it pointing to:

<%= link_to "Follow", new_user_friendship_path(friend_id: @user), class: "btn btn-info btn-sm" %>

I was finally able to resolve this issue.