2016-12-14 58 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]值。

相關問題