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

Saving Models and Redirections

I'm work on the first code challenge which states:

"Set the flash success message to "Friendship created" if the @user_friendship is saved."

My code:

class UserFriendshipsController < ApplicationController

  def create
       if 
          @user_friendship.save
           flash[:success] = "Friendship created"
   end              
  end
end

it throws:

`create': undefined method `save' for nil:NilClass (NoMethodError)

I've tried it without the if statement and the code still doesn't pass. Am I missing something simple here?

2 Answers

@user_friendship = current_user.user_friendships.new(friend: @friend)

When I add this line to the code the response from the challenge is:

Bummer! There was no flash success message set.

but there is obviously a flash[:success] message set.

I am having the same issue, if someone has an answer to this it would be greatly appreciated!