I am trying to show a Flash message by Ajax, since I have a modal that when closing I wish to notify the user that the action was done, I use Foundation 6, and unfortunately this version still needs to be added fundamental things like "Toast" messages from Materialize, LightBox, since they are only in version 5, returning to the point, I can not get the message after performing the action, then my code:
layouts / _alert.html.erb
<% if notice %>
<div class="callout small notice">
<%= notice %>
</div>
<% end %>
<% if alert %>
<div class="callout small alert">
<%= alert %>
</div>
<% end %>
_form.html.erb
<div class="callout">
<h6>Formulario de contacto</h6>
<%= form_for @contact, remote: true, authenticity_token: true do |f| %>
<% if @contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@contact.errors.count, "error") %> prohibited this @contact from being saved:</h2>
<ul>
<% @contact.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_field :title, placeholder: "Titulo del mensaje" %>
</div>
<div class="field">
<%= f.text_area :content, placeholder: "Contenido del mensaje", :rows => 10, :cols => 120 %>
</div>
<div class="actions">
<%= f.submit "Enviar mensaje", class: "button" %>
</div>
<% end %>
</div>
create.js.erb
<% if @home.errors.empty? %>
$('#exampleModal1').foundation('close');
$(".notice").html('<%= j render partial: "layouts/alerts" %>');
<% else %>
$(".alert").html('<%= j render partial: "layouts/alerts" %>');
<% end %>
contact_controller.rb
def create
@contact = Contact.new(contact_params)
@contact.email = current_enterprise.email
respond_to do |format|
if @contact.save
ContactMailer.contact_email(@contact).deliver
format.html { redirect_to root_path, notice: 'Contact was successfully created.' }
format.js { flash[:notice] = "El mensaje ha sido enviado" }
format.json { render :show, status: :created, location: @contacts }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end