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

Luca Yesupatham Daniel
Luca Yesupatham Daniel
8,714 Points

Creating Friendship UI Form

Hello,

I'm stuck in this code challange "Saving Models and Redirecting". It asks me "Set the flash success message to "Friendship created" if the @user_friendship is saved."

I tought something like this:

class UserFriendshipsController < ApplicationController

def new

if params[:user_friendship]

  flash[:success] = "Friendship created"

end

end

Thank you in advance for the help :)

9 Answers

Matt West
Matt West
14,545 Points

Right got this one sorted for you.

def create
    @user_friendship = UserFriendship.new(params[:user_friendship])
    if @user_friendship.save
        flash[:success] = "Friendship created."
    end
end

The question isn't very helpful as it doesn't tell you that you need to create a new UserFriendship instance first.

You also need to change the action from new to create and there needs to be a full-stop at the end of the flash message.

It's almost as though there should be a task prior to this one in the code challenge.

Jon Thomas
Jon Thomas
8,916 Points

Yea, we really need some hints during these challenges. There's no way beginners can remember all that syntax.

Or the tasks need to be smaller chunks of code. I'd rather do 10 tasks that break it up and make easier to challenge myself and remember things.

I definitely agree with you Jon! I would say I'm more than a beginner, but in the later stages of building this app I am absolutely and completely lost, especially as the instructors work at speed to condense the lessons into 10-15 minutes. I felt like the most basic stages were overly-dummied down and the more complicated material is whizzed through at breakneck speeds.

Eric Miller
Eric Miller
2,902 Points

Anser at bottom... https://teamtreehouse.com/forum/issue-completing-code-challenge

This code challenge REALLY needs to be reworked, the instructions do not tell you the correct specifics (such as a missing period) that cause it to fail... this leads you into second guessing yourself and understanding of the material rather than fixing a typo. The redirect should be specified in which situation they should be redirected_to profile (upon success or failure of save).

This whole section was very frustrating... I found my self pausing the video constantly to even type/copy along with the lesson rather than actually absorb the material... way too fast.

Matt West
Matt West
14,545 Points

Hi Luca,

Try:

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

I haven't actually done this code challenge so I'm flying a little blind with this one.

Luca Yesupatham Daniel
Luca Yesupatham Daniel
8,714 Points

Hi Matt,

first thank you for the fast reply. I've already tried that approach but it show me the error message.

I've also tried to play with it with

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

But with no result :( (your code is what I initially tought it was right)

Kelley Kent
Kelley Kent
33,450 Points

I have also had trouble with this question and not matter what I change, I keep getting it wrong. (I tried all three ways listed above) plus changing the def from new to create

Luca Yesupatham Daniel
Luca Yesupatham Daniel
8,714 Points

Oh I see, I definetely misunderstood the task XD. Thank you very much Matt! (at least the @user_friendship.save part was right :P)

Yeah, I bombed on this one as well, had to hit google up. Really had no idea how to solve this.

I also got stuck here. Thanks for the help.