2016-12-14 57 views
1

我有一个模式,但是当我关闭模式时,我需要显示一个Flash消息来验证该动作是否已执行,当我有这个代码,但我不能得到它,我正在使用基金会,但基金会doesn'吨有 “面包” 或 “警报” 像实现或引导,这里是我的代码:如何通过Rails显示ajax的Flash消息?

布局/ alerts.html.erb

<% if notice %> 
    <div class="callout small notice"> 
    <%= notice %> 
    </div> 
<% end %> 

<% if alert %> 
    <div class="callout small alert"> 
    <%= alert %> 
    </div> 
<% end %> 

contact_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: "Title" %> 
    </div> 

    <div class="field"> 
     <%= f.text_area :content, placeholder: "Content", :rows => 10, :cols => 120 %> 
    </div> 

    <div class="actions"> 
     <%= f.submit "Send", class: "button" %> 
    </div> 
    <% end %> 
</div> 

create.js.erb

<% if @contact.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] = "The message was sent" } 
     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 

回答

1

你已经解决了这个毫无疑问,但对于其他人谁在这里结束了通过谷歌:

您的代码应该可以工作,但您需要将noticealert传递给您的alerts.html.erb部分。

​​

您可能还需要在你的控制器不成功保存到设置flash[:alert]值。

相关问题