How to display a Rails flash notice upon redirect?

后端 未结 6 1255
天命终不由人
天命终不由人 2021-01-31 01:21

I have the following code in a Rails controller:

flash.now[:notice] = \'Successfully checked in\'
redirect_to check_in_path

Then in the /check_

6条回答
  •  执笔经年
    2021-01-31 01:47

    If you are using Bootstrap, this will display a nicely-formatted flash message on the page that's the target of your redirect.

    In your controller:

    if my_success_condition
      flash[:success] = 'It worked!'
    else
      flash[:warning] = 'Something went wrong.'
    end
    redirect_to myroute_path
    

    In your view:

    <% flash.each do |key, value| %>
      
    <%= value %>
    <% end %>

    This will produce HTML like:

    It worked!

    For available Bootstrap alert styles, see: http://getbootstrap.com/docs/4.0/components/alerts/

    Reference: https://agilewarrior.wordpress.com/2014/04/26/how-to-add-a-flash-message-to-your-rails-page/

提交回复
热议问题