views:

406

answers:

4

I have the following snippet of code in my controller

  def create
    @message = Message.new(params[:message])
    @message.message = h(@message.message)
    if @message.save
       flash[:message] = "Message Sent. Thank You for Contacting Me"
    else
       flash[:message] = "OOps Something went wrong"
    end
    redirect_to :action => 'contact'
  end

When I try to display the flash message in the contact form it doesnot display. I looked up possible solutions, but they dont seem to work. Any ideas what is going wrong?

A: 

The flash hash can contain any set of messages you want to save until the next render. Scaffolds usually use the contents of flash[:notice] for notification messages. If you didn't use a scaffold to generate your web page you will have to add <%= flash[:notice]%> to your views.

You're setting flash[:message] in your controller. So it's not going to show up anywhere in your view unless your view contains <%= flash[:message]%> somewhere.

Your possible solutions are change all occurrences of flash[:message] to flash[:notice] in your controller or add <%= flash[:message]%> to any views that this action could render.

EmFi
I had put that in my view, it did not work, Also the next possibility I changed flash[:message] to flash[:notice] and put <pre><%= flash[:message]%></pre> in the view, but that dint work too. Any other suggestions?
Shiv
I thought a flash message persists even after a redirect. Correct me if I am wrong.
Shiv
It might be getting cleared because of the redirect. Does show redirect anywhere?
EmFi
whatever key you use in the controller has to match what appears in the view. Also which views did you add the `<%=flash[:message]%>` to?
EmFi
let me explain I have a view contact which displays a contact form, the action goes to the same controller but create method, the code for create is above. No other redirects, except the one in create.
Shiv
contact.html.erb
Shiv
+1  A: 

Not saying that you wouldn't have tried it, but if I were you I would do something down the lines like

<% if flash[:messsage].blank? %>
  <h1> flash hash is blank </h1>
<% end %>

If you see the "flash hash is blank" in your browser you know what it means.

EDIT:-

Something from the docs "Just remember: They‘ll be gone by the time the next action has been performed." Try this in your controller

flash.keep(:message) #keep the flash entry available for the next action
Anand
yup tried that, but what I dont understand is why it is being cleared?
Shiv
nope doesnt work.
Shiv
+2  A: 

Your controller is redirecting to :action => 'contact'. Ensure that the template being rendered for that action has the flash notice output.

<%= flash[:message] %>

Also, you may want to use render :action ... vs redirect_to :action .... Save yourself a request.

nowk
yeah the issue is if I use flash.now[:message] = "Blah Blah" then it works with a render action. But just out of curiosity I was wondering why it doesnt work with the redirect action. And yeah the solution you mentioned is there in contact.html.erb.
Shiv
Well flash.now only works for the current state of the action, once you move from it gets cleared. So flash[..] would be what you want to use if you need it in on another request. Since your redirecting, and flash[..] does not work. I wonder if you might have some session problems with your app.
nowk
thats interesting, could be a sessions problem. Let me look into that.
Shiv
I guess it was a sessions problem after all.
Shiv
A: 

Hey Guys,

Thanks for your help, I finally got it working, the answer was like nowk suggested, I reset the session key and secret, restarted the server and it worked like a charm.

Shiv