Flash [: notice] does not work in Rails

I have the following code snippet in the 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 a flash message in the form of a contact, it does not appear. I was looking for possible solutions, but they don't seem to work. Any ideas what is going wrong?

+3
source share
3 answers

Your controller is redirected to: action => 'contact'. Verify that the flash output is output for the template displayed for this action.

<%= flash[:message] %>

Alternatively, you can use render: action ... vs redirect_to: action ... Save the request.

+16
source

- , . [: ] . -, <%= flash[:notice]%> .

[: ] . , <%= flash[:message]%> -.

flash[:message] flash[:notice] <%= flash[:message]%> , .

+5

, , , -

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

If you see that the "flash hash is empty" in your browser, you know what that means.

EDIT : -

Something from the documents "Just remember: they will disappear by the time the next action is completed." Try this on your controller

flash.keep(:message) #keep the flash entry available for the next action
+2
source

Source: https://habr.com/ru/post/1723603/


All Articles