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 Mutual Friendship

SyntaxError in UserFriendshipsController#new

SyntaxError in UserFriendshipsController#new

app/models/user_friendship.rb:35: syntax error, unexpected keyword_end, expecting end-of-input

Hi all, I keep getting the error message above. I am following along with the 'Mutual Friendship' tutorials and I can't seem to figure out what the problem is. I have added and updated the ends however, nothing is working.

User_Friendships/Controller

class UserFriendshipsController <    ApplicationController
before_filter :authenticate_user!

  def index
    @user_Friendships = current_user.user_friendships.all
  end   

  def accept
    @user_friendship = current_user.user_friendships.find(params[:id])
    if @user_friendship.accept_mutual_friendship!
      flash[:success] = "You are now friends with #   {@user_friendship.friend.name}"
      redirect_to user_friendships_path
    else
      flash[:error] = "That friendship could not be accepted"
      redirect_to user_friendships_path
    end
  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 =     UserFriendship.request(current_user, @friend)
      if @user_friendship.new_record?
        flash[:error] = "There was a problem creating this friend request."
      else
        flash[:success] = "Friend request sent."
      end
      redirect_to user_path(@friend)
    else
      flash[:error] = "Friend required"
      redirect_to root_path
    end
  end

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

  def user_friendship
    params.require(:user_friendship).permit(:user_id,    :friend_id, :user, :friend, :state, :user_friendship)
 end    
end

User Model

class UserFriendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'

  state_machine :state, initial: :pending do

    after_transition on: :accept, do: :send_acceptance_email

    state :requested

    event :accept do
        transition any => :accepted
    end
end

    def self.request(user1, user2)
    transaction do
        friendship1 = create!(user: user1, friend: user2, state: 'pending')
        friendship2 = create!(user: user2, friend: user1, state: 'requested')

        friendship1.send_request_email
        friendship1
     end    
   end


 def send_request_email
    UserNotifier.friend_requested(id).deliver
 end

def send_acceptance_email
    UserNotifier.friend_request_accepted(id).deliver
end
end

1 Answer

Looks like you may have an extra end in your UserFriendship model.

Wow! I completely missed that. Thank you Geoff

Hi Geoff. Would you happen to know why when clicking on the 'accept friendship' button the friendship status does not update for that user, it remains as 'Friendship Requested' still? I have been trying to figure this out for some time now. Thanks in advance. I have been posting and asking everywhere.

I hate to admit this but i actually haven't been through this course myself. Maybe Jason Seifer or Andrew Chalkley could help out?

Gotcha. Thank you for the previous help! Is there a way I can ask either one for help? I posted a question in regards to this on the forum. If not I will find a way. Thanks again for the help!

Mentioning them above should notify them like a Twitter mention. So hopefully one of them will show up on their next pass of the forums. Good luck on your courses!

Thanks!

I figured it out. Thanks guys. The code works when I tweaked it to:

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