Here is my action to create. It creates a new Message instance, which is verified using model validation.
Then there is a simple if else loop that sends a message if the model check has been completed and displays a βnewβ view if they want to send another. If the model has not been validated, it simply displays the βnewβ view again.
A flash hash is also populated with relevant information, a successful message and a warning message if the message was successful or not.
def create @message = Message.new(message_params) if @message.valid? Contactform.contact(@message.name, @message.town, @message.email, @message.content).deliver flash[:success] = "Sent message" render 'new' else flash[:alert] = "Alert" render 'new' end end
However, I do not think that I am doing it right because the messages remain in flash. They are not cleaned after they have been viewed once.
How can I make sure the flash is clean, as it should be?
Update
So an error occurs. I send a message successfully, and I redirect to a "new" look, and get a "successful" flash. I am trying to send an erroneous message, and I am redirected to the "new" view, and I get both a "successful" flash and a "warning". Very confusing for the user!
In the second, I go to another page, both flashes disappear, but I think about this problem. Something has to do with returning to the same page that Rails does not know to clear the flash. Can I clear the flash?
source share