Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
How to update a modal DOM object after method call with JS on Rails 5.2?

I am trying to achieve something that sounds simple, but that fails in this particular context.

A Task instance is displayed in a modal window. The modal Show view includes a form which, when submitted, triggers the verify method in the tasks_controller. The method updates a field, and the change should be reflected in the current Show view.

Unfortunately, Rails raises an UnknownFormat error, claiming for a html template.

I also tried to render the javaScript snippet directly inside the method with: render js: "document.getElementById('task-git-hash').value = ('#{ @task.git_hash }');" but the code is rendered literally as new html page, instead of being executed in the current view.

show.html.erb modal view (extract)

  <%= form_with model: @task, url: :verify_task, method: :post, html: {id: "verify_form"}, remote: true do |f| %>
    <div class="row mat-form-row">
      <div class="mat-form-field col-md-4">
        <label for="task-git-hash" class="mat-form-field-label">
          <%= t('tasks.show.GitHash') %>
        </label>
        <input id="task-git-hash" class="mat-input-element" readonly
          value="<%= @task.git_hash %>" />
      </div>
        <div class="mat-form-field col-md-6">
          <%= f.label :verify_on_technology_id, t('tasks.show.Technology'), class: "mat-form-field-label" %>
          <%= f.grouped_collection_select :verify_on_technology_id,
              values_options_for('Technologies', 1, @task.technology_id), # group
              :subs,                                 # descendants
              :code,                                 # group label
              :id, :code,                            # option key & label
              {:include_blank => false},
              { class: "mat-input-element select2-candidate" } %>
        </div>
        <div class="mat-form-field col-md-2">
          <%= f.label :git_hash, t('tasks.show.GetHash'), class: "mat-form-field-label" %>
          <button class="mat-flat-button mat-button-base mat-primary">
            <%= t('Verfiy') %>
          </button>
        </div>
    </div>
  <% end %>

tasks_controller method

  def verify
    ### Retrieved by Callback function
    @task.update_attribute(:git_hash, Time.now.strftime("Tested at %H:%M"))
  end

verify.js.erb view

document.getElementById("task-git-hash").value = ("<%= @task.git_hash %>");

How can I manage to update this field in the current view when the form is validated?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda