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

Routing Error when flash is after redirect_to

So I just finished the "Creating the Friendship" tutorial and hit a bit of a snag.

In the "create" method we have the if-else statement and in the instance where the friendship is created successfully and you are redirected to the friend's profile_path I had my code written this way:

redirect_to profile_path(@friend)
    flash[:success] = "You are now friends with #{@friend.full_name}"

This code did not cause any of my tests to fail, however when I did a sanity check of the site I noticed that adding the friend caused a Routing Error.

No route matches {:controller=>"profiles", :action=>"show", :id=>nil}

Now, I was able to fix the problem by switching the flash and the redirect to where flash occurred before the redirect_to, but my question is why?

I feel like in order of occurrence the flash should happen after the redirect since the error or success is displayed on the page, but apparently I'm wrong. Is it because the flash has to cue the redirect or something?

Any insight would be very helpful. Thank you!

1 Answer

I'm fairly certain that the order of flash and redirect doesn't matter, since the flash simply sets a variable under :flash in your session. The routing error you received actually makes it look like @friend was nil when you tested it originally, since the routing error showed an id of nil, so that may have actually been the cause of your error, not the order of flash and the redirect.

Thank you for your response, Alan! Well, that makes sense since I believe the path is dependent upon the id, and I agree that I wouldn't think the order would matter.

However, I'm still confused as to why I received the error when the ONLY thing I changed was the order of the flash and the redirect. After I made that switch, everything worked fine, no errors, failures or visual failures within the running application.

Wouldn't the instance variable have still been nil in both cases and wouldn't I have received the same error regardless of which was first?

You know, I'm not totally sure. Have you tried switching the order now that everything's working to see what happens? I've definitely had stranger things happen during a debugging session.

Yes, when I first opened this discussion I tried switching the flash to above and below the redirect and I would always get the same result: if the flash was above the redirect, I would get the routing error and if it was below everything would work fine.

Since my code was working in testing and practically (as long as it was in the right order), I continued going through the tutorial videos, even though the problem still confused me. Now if I revisit the problem I had before (switching the flash above or below) I don't have the same result.

I'm not sure what fixed the problem, but at one point I deleted my db that was in production and started my user base from scratch (due to another issue I was having that I was able to resolve). Now that I'm not able to recreate the issue it's all speculation, but it could make sense that a user created previously could have a nil value floating around for some reason or another, I guess.

In any case, I hate not truly resolving this, but since I'm not able to recreate the problem I'm not sure what else to do. Thanks for working on this with me, Alan!

Yeah, I've had things like that magically clear up before too. Definitely let us all know if you bump into any other weirdness!