Surge
Surge

Reputation: 256

Flash message not working on Rails 4 after a redirect

  def show
    email_address = EmailAddress.find_by(confirmation_token: params[:id])
    if email_address.confirm!
      flash[:notice] = "Thanks for confirming your email address (#{email_address.email})"
      flash.keep
      redirect_to user_account_path
    else
      flash[:error] = "Invalid confirmation token"
      redirect_to root_path
    end
  end

Trying to redirect from this controller to another controller and i want to flash "Thanks for confirming your email address (#{email_address.email})" but am not seeing the flash message on the page am redirecting to.

Tried hardcoding a flash message on the other controller and it got displayed. It's only when I do a redirect the flash message doesn't go through.

Upvotes: 0

Views: 1388

Answers (2)

raj_on_rails
raj_on_rails

Reputation: 144

You might be missing flash code in layout.

Add the following in layouts/application.html.erb in body section:

<% flash.each do |name, msg| -%>
      <%= content_tag :div, msg, class: name %>
    <% end -%>

Upvotes: 0

Maxim
Maxim

Reputation: 9961

You can use redirect_to and flash together:

redirect_to user_account_path, notice: "Thanks for confirming your email address (#{email_address.email})"

Try to use this way to make redirect and show flash notice.

If this is doesn't work then probably your problem related to rendering flash message. Check content of flash helper in view and code which render it.

Upvotes: 1

Related Questions