Working with modals and Ajax

1

I need to know how I can process the data collected in a modal and then update the selected field.

Being more concrete: In a view of my app I have a list of workers, which provides the possibility of adding a new one, this process I want to do it through a modal, once the form is sent, only the list of workers.

With ASP.Net MVC I have done it through microsoft.jquery.unobtrusive.ajax and it seems to me that with rails it should not be complex.

I was looking for link where a similar example appears and I have made it work in part. What I need is the following:

  • That the modal one closes when creating a worker satisfactorily, because it does not do it
  • Let me show validation errors in the modal that does not either.
  • Clean the form.
  • this is my code: app / views / trabajadors / index.html.erb

     <h1>Trabajadores</h1>
    
    <table class="ui blue table">
      <thead>
        <tr>
          <th>Nombre</th>
          <th>Apellidos</th>
          <th colspan="3"></th>
        </tr>
      </thead>
    
      <tbody id = "trabajadores">
        <% @trabajadors.each do |trabajador| %>
          <tr>
            <td><%= trabajador.nombre %></td>
            <td><%= trabajador.apellidos %></td>
            <td>
              <a data-tooltip="Detalles del Trabajador" data-position="top right" href="<%= trabajador_path(trabajador) %>"><i class="big unhide icon"></i></a>
              <a data-tooltip="Editar Trabajador" data-position="top center" href="<%= edit_trabajador_path(trabajador) %>"><i class="big write icon"></i></a>
              <a data-tooltip="Eliminar Trabajador" data-position="top left" data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="<%= trabajador_path(trabajador) %>"><i class=" big remove icon"></i></a>
          </tr>
        <% end %>
      </tbody>
    </table>
    
    <br>
    
    <button class="ui green button" id="nuevo">Nuevo Trabajador</button>
    
    <div class="ui modal">
      <div class="header">Nuevo Trabajador</div>
      <div class="image content">    
        <%= form_for(@trabajador, remote: true, html: { class: "ui form" }) do |f| %>
          <% if @trabajador.errors.any? %>
          <div id="error_explanation">
          <h2><%= pluralize(@trabajador.errors.count, "error") %> prohibited this trabajador from being saved:</h2>
    
        <ul>
         <% @trabajador.errors.full_messages.each do |message| %>
          <li><%= message %></li>
         <% end %>
        </ul>
      </div>
      <% end %>
    
      <div class="field">
        <label>Nombre</label>
        <%= f.text_field :nombre %>
      </div>
      <div class="field">
        <label>Apellidos</label>
        <%= f.text_field :apellidos %>
      </div>     
    
    </div>
      <div class="actions">
      <div class="ui black deny button">
      Cerrar
      </div>     
      <%= f.submit %>     
      <% end %>
    </div>
    </div>
    </div>
    
    <script>
      $('.ui.modal')
      .modal({
        closable: false,
        blurring: true
       })
      .modal('attach events', '#nuevo', 'show'
     );
    </script>
    

    app / views / workers / _worker.html.erb

     <tr>
        <td><%= trabajador.nombre %></td>
        <td><%= trabajador.apellidos %></td>
        <td>
        <a data-tooltip="Detalles del Trabajador" data-position="top right" href="<%= trabajador_path(trabajador) %>"><i class="big unhide icon"></i></a>
        <a data-tooltip="Editar Trabajador" data-position="top center" href="<%= edit_trabajador_path(trabajador) %>"><i class="big write icon"></i></a>
        <a data-tooltip="Eliminar Trabajador" data-position="top left" data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="<%= trabajador_path(trabajador) %>"><i class=" big remove icon"></i></a>
      </tr>
    

    app / controllers / trabajadors_controller.erb

    def index
    @trabajadors = Trabajador.all
    @trabajador = Trabajador.new
    end
    
    def create
    @trabajador = Trabajador.new(trabajador_params)
    
    respond_to do |format|
      if @trabajador.save
        format.html { redirect_to @trabajador, notice: 'Trabajador was successfully created.' }
        format.js {}
        format.json { render :show, status: :created, location: @trabajador }
      else
        format.html { render :new }
        format.json { render json: @trabajador.errors, status: :unprocessable_entity }        
      end
    end
    

    end

    app / views / trabajadors / create.js.erb

    $("<%= escape_javascript(render @trabajador) %>").appendTo("#trabajadores");
    
        
    asked by Vicente Almea 24.05.2017 в 23:27
    source

    1 answer

    0

    I recommend you check jquery. To read data from a form, eg:

    <form>
    <input type="text" id="txtDato" name="txtDato" />
    <button type="button">Crear</button>
    </form>
    

    You read the value of the input like this: var dato1 = $("#txtDato").val();

    Then you do what you need in javascript and to persist the data in the server you use ajax.

    The question is very general because of this the answer too.

    I hope I guide you

    Check JQuery, Ajax in JQuery, basically solve with selectors and ajax.

    Greetings

        
    answered by 25.05.2017 в 02:06