title: Show flash message after submitting form using ajax
body: <p>Im building my first app using rails and also im trying to add some ajax functionality on it. I have a signup form in my root url in order to create users but i cant find a way to show my success message after a user has been created. I can see the json response correctly but not the flash message. Is there something im missing in my code?</p>
  
  <pre><code># Controller
  
  def create
    @user = User.new(user_params(CREATE_PARAMS))
  
    respond_to do |format|
      if @user.save
        @user.send_activation_email
        format.html { flash[:info] = "Success!!"
                      redirect_to root_url
        }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
  
  # application.html.erb
  
  &lt;body&gt;
    &lt;div class="container"&gt;
      &lt;div class="message-notification"&gt;
        &lt;%= render 'shared/alert_messages' %&gt;
      &lt;/div&gt;
      &lt;%= yield %&gt;
    &lt;/div&gt;
  &lt;/body&gt;
  
  # shared/_alert_messages.html.erb
  
  &lt;% flash.each do |message_type, message| %&gt;
    &lt;%= content_tag(:div, message, class: "alert-message alert-message-#    {message_type}") %&gt;
  &lt;% end %&gt;
  
  # Handling errors users.coffee
  $(document).on "ajax:error", "form#new_user", (event, data, status, xhr) -&gt;
    $("form#new_user").render_form_errors "user", data.responseJSON
  
  $.fn.render_form_errors = (model_name, errors) -&gt;
    form = this
    this.clear_form_errors()
  
    $.each errors, (field, messages) -&gt;
      input = $('input[name="' + model_name + '[' + field + ']"]');
      input.closest(".form-group").addClass("has-error")
      input.parents(".form-group").append('&lt;span class="help-block"&gt;'  + 
        $.map(messages, (m) -&gt; m.charAt(0).toUpperCase() + 
        m.slice(1)).join("&lt;br /&gt;") + "&lt;/span&gt;")
  
  $.fn.clear_form_errors = () -&gt;
    this.find(".form-group").removeClass("has-error")
    this.find("span.help-block").remove()
  </code></pre>