1
votes

I have created rails 5 application and I have the following code in controller and views for showing flash messages. I saw many question like this but I could not solve the problem.

controller side

    respond_to do |format|
      if @trigger.update(trigger_params)
        flash[:success] = 'Trigger was successfully updated.'
        format.html { redirect_to edit_project_trigger_path(@trigger, project_secret_key: @project.secret_key) }
        format.json { render :show, status: :ok, location: @trigger }
      else
        error = ''
        @trigger.errors.full_messages.each do |msg|
          error += "#{msg} <br>"
        end
        flash.now[:error] = error
        format.html { render :new }
        format.json { render json: @trigger.errors, status: :unprocessable_entity }
      end
    end

view side

<% if flash[:error].present?%>
    <div class="alert alert-danger alert-message alert-message-common" role="alert">
      <%= flash[:error].html_safe %>
    </div>
<% end %>
<% if flash[:notice].present? %>
    <div class="alert alert-success alert-message alert-message-common" role="alert">
      <%= flash[:notice].html_safe %>
    </div>
<% end %>
<% if flash[:alert].present? %>
    <div class="alert alert-danger alert-message alert-message-common" role="alert">
      <%= flash[:alert].html_safe %>
    </div>
<% end %>
<% if flash[:success].present?%>
    <div class="alert alert-success alert-message alert-message-common" role="alert">
      <%= flash[:success].html_safe %>
    </div>
<% end %>

It's show when using alert but success messages are not shown. Is there any problem in rails 5 or any problem in my code ?

I hope alert working because of page render and success not working because of redirect.

4
You are not setting any value in flash[:success] in the code you have shown us... is there more code than this? under what circumstances would you expect to see something in flash[:success] ? - Taryn East
@TarynEast I want show success message once data updated successfully in db. I was used flash[:notice] and flash[:success] in same purpose. - empr
1. Change css class for all types to alert-danger(Since alert is working) for testing purpose. 2. Then try this solution. - Omkar
@Omkar still same issues and I hope there is no issues in css. - empr

4 Answers

0
votes

Only alert and notice flash keys are supported by default.

To add success:

class ApplicationController
  add_flash_types :successs

Reference: Ruby Guides

-1
votes

try this:

format.html { redirect_to edit_project_trigger_path(@trigger, project_secret_key: @project.secret_key), :success => 'Trigger was successfully updated.' }
-1
votes

This should work:

format.html { redirect_to edit_project_trigger_path(@trigger, project_secret_key: @project.secret_key), flash: {success: 'Trigger was successfully updated.'} }

For some reasons, in rails you should you either notice or alert keys in redirect_to method, but if you need success or something else, you should use nested hash in flash key.

Check here http://blog.remarkablelabs.com/2012/12/register-your-own-flash-types-rails-4-countdown-to-2013

-1
votes

The Rails docs for redirect_to mention that "There are two special accessors for the commonly used flash names alert and notice as well as a general purpose flash bucket." You need to use regular flash syntax for any others.