Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise an alert in rails controller without redirect

I just want to flash an notice/error if the message is/isn't saved, without any redirect, how can I have no redirect:

  respond_to do |format|
  if @message.save
    format.html { redirect_to request.referer, :notice => 'Message sent!' } #dont want redirect
  else
    # error message here
  end
like image 501
hagope Avatar asked Sep 23 '11 21:09

hagope


2 Answers

Use flash.now:

if @message.save
  flash.now[:notice] = 'Message sent!'
else
  flash.now[:alert] = 'Error while sending message!'
end

respond_to do |format|
  format.html { # blahblah render }
end
like image 164
Benoit Garret Avatar answered Nov 15 '22 23:11

Benoit Garret


In rails 5, you can do:

format.html { redirect_to request.referer, alert: 'Message sent!' }
like image 36
Obromios Avatar answered Nov 15 '22 22:11

Obromios