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

375
Visualizações
RoR-STimulus-Fetch-API : how to add something fetched to DB

I am trying to add something I fetched from an API to my RoR DB.

So I have :

  • my view :

    <div data-controller="album">
    
      <form data-album-target = "form"
         data-action="submit->album#update">
      <input  type="text"
            class="form-control"
            data-album-target="input">
    
      <div data-album-target="results"></div>
    
      </div>
    
  • and my stimulus controller

      import { Controller } from "stimulus"
    
         export default class extends Controller {
        static targets = ["results", "input", "form"]
    
    update(event) {
      event.preventDefault();
      this.resultsTarget.innerHTML = "";
      const artistName = this.inputTarget.value;
      const url = `https://theaudiodb.com/api/v1/json/2/search.php?s=${artistName}`;
      fetch(url)
        .then(response => response.json())
        .then((data) => {
          this.findIdArtist(data);
        });
    }
    
    findIdArtist(data){
      data.artists.forEach((result) => {
        const idArtist = result.idArtist;
        const url2 = `https://theaudiodb.com/api/v1/json/2/album.php?i=${idArtist}`;
        fetch(url2)
          .then(response => response.json())
          .then((data) => {
            data.album.sort(function (a, b) { return a.intYearReleased - b.intYearReleased }).forEach((list) => {
              const album =
                `<div class="card">
                  <img src=${list.strAlbumThumb}>
                  <div class="albumtitle">
                    ${list.strAlbum}
                  </div>
                  <div class="albumyear">
                    ${list.intYearReleased}
                  </div>
                </div>`
              this.resultsTarget.insertAdjacentHTML("beforeend", album)
            })
          });
      });
    }
    }
    

How could I have a "+" button which adds the albumID to my RoR database ? I tried with a POST fetch but I can't get it to work.

Any idea ?

Thanks !

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