Leahcim
Leahcim

Reputation: 42069

Trouble setting flash messages in rails

I've forked a project on GitHub that has some flash messages. For example, with Twitter sign in the project I forked comes with this red error message for failed sign in

  flash[:error] = "Sign in with Twitter failed"

I've also found the class in the CSS that makes it red. I want to use this error message elsewhere but I'm having problems.

For example, when I tried to do this

redirect_to show_path, flash[:error] => "Twitter's saying you're trying to post the same message twice"

It's simply not posting the message to Twitter and giving no error message. When I change => to = it breaks the whole application (when I try to post twice) giving me this message:

 can't convert Symbol into String

Even stranger (to me), I have no problem with :notice. This was fine.

redirect_to show_path, :notice => "Your Tweet was posted!"

Can anyone explain why this is happening? This is the project on GitHub.

Upvotes: 2

Views: 3303

Answers (3)

bassneck
bassneck

Reputation: 4043

Yeah, I've been there too :)

@jsinger is partially right. You need to pass a symbol instead of the flash[] hash. But there's a catch: redirec_to supports only two shorthand symbols: :notice and :alert. There is a third, a general one: :flash.

redirect_to show_path, :flash => { :success/:error/:whatever => "your flash message" }

I wonder what reasoning is behind this.

APIdock

Upvotes: 3

Wahaj Ali
Wahaj Ali

Reputation: 4113

Did you try?

flash.now[:notice] = "some text"

Upvotes: 0

jsinger
jsinger

Reputation: 807

Note how in your example with notice, you're using the raw :notice symbol in the hash arg of redirect_to instead of flash[:notice]. In your code with the error flash, you're trying to use flash[:alert]. You need to pass just the symbol, so try

redirect_to show_path, :error => "Twitter's saying you're trying to post the same message twice"

You can also set the alert flash prior to the redirect_to call:

flash[:error] = "Twitter's saying you're trying to post the same message twice"
redirect_to show_path

Upvotes: 4

Related Questions