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

Nelly Nelly
Nelly Nelly
7,134 Points

Stuck on challenge task 2 of 3 Set the flash error message to "There was a problem." if the @user_friendship is not s

I have this "Bummer! There was no flash error message." with that code? What 's wrong? thanks for your help :)

class UserFriendshipsController < ApplicationController
  def create
     if 
     @user_friendship = current_user.user_friendships.new(friend: @friend)
     @user_friendship.save
       flash[:success] = "Friendship created."
     else
       flash[:error] ="There was a problem."
     end
  end
end

5 Answers

Ilya Dolgirev
Ilya Dolgirev
35,375 Points

You have to look carefully on your code :) You've just misplaced assignment with condition. Generally, you need to assign a variable first (move it outside from condition block), then check the condition about successful saving.

I don't want to place the correct code right now, but if you still stuck let me know.

Nelly Nelly
Nelly Nelly
7,134 Points

almost got it now iam stuck on thrid question

Redirect to the profile_path of the friend.

class UserFriendshipsController < ApplicationController
   def create
     @user_friendship = current_user.user_friendships.new(friend: @friend)

     if 
     @user_friendship.save
       flash[:success] = "Friendship created."
       redirect_to profile_path(@friend)
     else
       flash[:error] ="There was a problem."
     end
  end
end
Ilya Dolgirev
Ilya Dolgirev
35,375 Points

What the error message? :)

Nelly Nelly
Nelly Nelly
7,134 Points

it said Bummer! No redirect occurred. :(

Ilya Dolgirev
Ilya Dolgirev
35,375 Points

The challenge said to just redirect to friend's profile, no matter you successfully save your friendship or not.

Nelly Nelly
Nelly Nelly
7,134 Points

OKay got it.... it ! passed the redirect_to after condition!

Thanks for your help ! Very intersting way to help!
I can be happy that I kinda found myself :smile:

Ilya Dolgirev
Ilya Dolgirev
35,375 Points

You're welcome :) IMHO, it's the best way to get something when you do it yourself even you get some tips. So, you can be proud! :) Good luck!

Nelly Nelly
Nelly Nelly
7,134 Points

thanks ! I may have other question in the futur :smile: