weixin_33744141 2016-11-02 00:21 采纳率: 0%
浏览 49

Flash消息(ajax)

Is it possible to show the errors of the form in the flash messages in ajax? I want to show the errors of the form when is not possible to save a record due to unique field.

my index

<!--<p id="notice"><%= notice %></p>-->
<h1>Empresas</h1>
<style>
.container {
}

</style>
<div class="container">
  <div class="row">
    <div class="text-center">
      <!-- Button trigger modal -->
      <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mynewempresa">
        Nueva empresa
      </button>
    </div>
  </div>

  <br>
  <br>



  <table id="empresas" class="display"><!--el id empresas es de datatables referenciado en empresas.coffe y display class es una clase de datatables-->
  <thead>

    <tr><!--active es para sombrear la fila-->
      <th>Clave</th>
      <th>Empresa</th>
      <th>Acción</th>
      <th></th>

    </tr>
  </thead>
    <tbody id="container_empresas">
      <%= render @empresas %><!--carga todos los empresas-->
</tbody>


</table>
<!-- Modal create action -->
<%= form_for(@empresa, remote: true, html: {class: "form-horizontal formulario-validado-create"}) do |f| %> <!--ajax remote: true-->
  <div class="modal fade" id="mynewempresa" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Agregar empresa</h4>
        </div>
        <div class="modal-body">

          <div id="flash_messages_placeholder" >

           </div>

          <div class="form-group">
            <%= f.label :IdEmpresa, "Clave:", class: "control-label col-md-2 " %>
            <div class="col-md-3">
              <%= f.text_field :IdEmpresa, class: "form-control empresa_clave",autofocus: true, minlength: "1", required: "true"  %>
            </div>
              <%= f.label :Empresa, "Empresa:", class: "control-label col-md-2" %>
            <div class="col-md-5">
              <%= f.text_field :Empresa, class: "form-control empresa_empresa",minlength: "4", required: "true"  %>
            </div>
          </div>





        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal" id="mynewempresaclose">Cerrar</button>
          <%= submit_tag "Crear", class: "btn btn-primary"%>
        </div>
      </div>
    </div>
  </div>
<%end%>
</div>

my controller

class EmpresasController < ApplicationController
  before_action :set_empresa, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_usuario!


  # GET /empresas
  # GET /empresas.json
  def index
    @empresas = Empresa.all
    @empresa = Empresa.new
    respond_to do |format|
      format.html
      format.csv { send_data @empresas.to_csv}
      format.xls #{ send_data @empresas.to_csv(col_sep: "\t") }
    end
  end

  def import
    Empresa.import(params[:file])
    redirect_to empresas_path, notice: "Sucursales importadas."
  end

  # GET /empresas/1
  # GET /empresas/1.json
  def show
  end

  # GET /empresas/new
  def new
    @empresa = Empresa.new
  end

  # GET /empresas/1/edit
  def edit
  end

  # POST /empresas
  # POST /empresas.json
  def create
    @empresa = Empresa.new(empresa_params)
    format.js { render 'shared/error_messages_js'}

    respond_to do |format|
      if @empresa.save
        format.html { redirect_to @empresa, notice: 'Empresa was successfully created.' }
        format.json { render :show, status: :created, location: @empresa }
        format.js {flash.now[:notice] = 'La Sucursal se ha creado de forma exitosa.'} #ajax


      else
        format.html { render :new }
        format.json { render json: @empresa.errors, status: :unprocessable_entity }
        format.js {flash.now[:alert] = 'Error al crear la sucursal.'} #ajax


      end
    end
  end

  # PATCH/PUT /empresas/1
  # PATCH/PUT /empresas/1.json



  def update
    respond_to do |format|
            ##use locals if multiple models
        ##add model errors
        flash.now[:error] = @empresa.errors.full_messages
        ##flash.now[:error] = @model.errors.full_messages.to_sentence

      if @empresa.update(empresa_params)
        format.html { redirect_to @empresa, notice: 'Empresa was successfully updated.' }
        format.json { render :show, status: :ok, location: @empresa }
        format.js {flash.now[:notice] = 'La Sucursal se ha actualizado de forma exitosa.'} #ajax


      else
        format.html { render :edit }
        format.json { render json: @empresa.errors, status: :unprocessable_entity }
        format.js {flash.now[:alert] = 'Error al actualizar la sucursal.'} #ajax


      end
    end
  end

  # DELETE /empresas/1
  # DELETE /empresas/1.json
  def destroy
    @empresa.destroy
    respond_to do |format|
      format.html { redirect_to empresas_url, notice: 'Empresa was successfully destroyed.' }
      format.json { head :no_content }
      format.js {flash.now[:notice] = 'La Sucursal se ha borrado de forma exitosa.'} #ajax


    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_empresa
      @empresa = Empresa.find(params[:idempresa])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def empresa_params
      params.require(:empresa).permit(:IdEmpresa, :Empresa)
    end
end

my application helper:

module ApplicationHelper
  ###application_helper.rb
   def flash_display
       Rails.logger.info "show flash message ajax======== "
     response = ""
     flash.each do |name, msg|
       msg=msg ###"<i class='fa fa-quote-left fa-border'></i>"+msg+"<i class='fa fa-quote-right fa-border'></i>"+"<button type='button' class='close' title='hide' data-dismiss='alert'><i class='fa-times-circle-o fa pull-right'></i></button>".html_safe
       response = response +
       content_tag(:div, msg, :id => "flash_#{name}",:class=>"alert alert-danger") do
           "#{msg}".html_safe
       end
     end
     flash.discard
     response
   end
end

##views/shared/error_messages_js.erb

$('#flash_messages_placeholder').html("<%= escape_javascript raw(flash_display) %>");
  • 写回答

1条回答 默认 最新

  • weixin_33726318 2016-11-02 03:03
    关注

    Yes..its possible..

    firstly,create a helper method that can be used to show flash messages on the view.

     ###application_helper.rb
        def flash_display
            Rails.logger.info "show flash message ajax======== "
          response = ""
          flash.each do |name, msg|
            msg=msg ###"<i class='fa fa-quote-left fa-border'></i>"+msg+"<i class='fa fa-quote-right fa-border'></i>"+"<button type='button' class='close' title='hide' data-dismiss='alert'><i class='fa-times-circle-o fa pull-right'></i></button>".html_safe
            response = response + 
            content_tag(:div, msg, :id => "flash_#{name}",:class=>"alert alert-danger") do 
                "#{msg}".html_safe  
            end     
          end
          flash.discard
          response
        end
    

    Secondly,In your controller code where you post data using remote=true,handle js response as shown below.

         respond_to do |format|
              ##use locals if multiple models
            ##add model errors
            ##flash.now[:error] = @model.errors.full_messages
            ##flash.now[:error] = @model.errors.full_messages.to_sentence
            flash.now[:error]="You cannot edit this Page"
            format.js { render 'shared/error_messages_js'}
           end
    
         ##views/shared/error_messages_js.erb
            $('#flash_messages_placeholder').html("<%= escape_javascript raw(flash_display) %>");
    

    So,the above js.erb file will look for an empty placeholder to show flash messages.So your form must also have a placeholder

        ###_form.html.erb
        <form action="<%= post_data_path(current_user)%>" method="post" data-remote="true">
        <div id="flash_messages_placeholder" > </div>
    
        ##input fields/submit button goes here
       <%end%>
    

    Hope it helps :)

    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题