I recently implemented an AJAX post request in my backend Rails application to upload a file. Upon successful posting, the response contains a JSON list of files.
Now, I face the challenge of reloading the file list on my 'detalhes.html.erb' view page, which displays the list as a partial.
Below is my controller method:
def upload_arquivo
@ponto_venda = PontoVenda.find(params[:pv_id])
nome = params[:key].split('/').last
@arquivo = Arquivo.new(nome: nome, endereco: params[:url], s3_key: params[:key], tamanho: params[:tam], user_id: params[:u_id], tag_id: params[:tag])
if @arquivo.save!
ArquivoPontoVenda.create(arquivo_id: @arquivo.id, ponto_venda_id: @ponto_venda.id, fachada: false)
response = { files: filtro_pv_arquivos, message: "O arquivo '#{nome}' foi salvo" }
else
response = { files: '', message: 'Ocorreu um erro ao salvar o arquivo, por favor, tente novamente' }
end
render json: response
end
And here's my AJAX post method at _arquivos_upload.html.erb
:
$.ajax({
url: 'some_url',
type: 'post', processData: false
})
.success(function(data) {
console.log(data.files.length);
// attempting to reload the partial after the POST request
$('#display-arquivos').html("<%= escape_javascript(render 'arquivos_buscados') %>");
});
The data.files contain the correct information, but I'm struggling to pass it to the rendered partial. Can someone suggest a better way to handle this?
Here's where I render the partial detalhes.html.erb
:
<div class="card" id="display-arquivos">
<%= render 'arquivos_buscados' %>
</div>
I have already explored various solutions including:
setting .html(data.files)
using js.erb file logic