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

Accept friendship button isn't showing up

This is my problem, when I click accept friendship and go to the edit user_friendship page the "Accept Friendship" doesn't show up.

app/views/user_friendships/edit.html.erb

<div class="page-header">
    <h1>Viewing Friendship</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 "Accept Friendship", class: 'btn btn-primary' %>
        <% end %>
    <% end %>
</div>

app/controllers/user_friendship_controller.rb

class UserFriendshipsController < ApplicationController
    before_filter :authenticate_user!

    def index
        @user_friendships = current_user.user_friendships.all
    end

    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 = UserFriendship.request(current_user, @friend)
            if @user_friendship.new_record?
                flash[:error] = "There was a problem creating that friend request."
            else
                flash[:success] = "Friend request sent."
            end
            redirect_to profile_path(@friend)
        else
            flash[:error] = "Friend required"
            redirect_to root_path
        end
    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.first_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

Hi Arthur,

Did you ever find a solution as to why the accept button was not being displayed? I am having the same issue. Thanks.

1 Answer

Hey Arthur-

Looks like you need to add an "=" sign to this line to not only evaluate teh ruby as a form but also to display it. Try changing this

 <% form_for @user_friendship, url: accept_user_friendship_path(@user_friendship), method: :put do |form| %>

to this

<%= form_for @user_friendship, url: accept_user_friendship_path(@user_friendship), method: :put do |form| %>

with the ='s sign.

-twiz