I have an ajax post to my backend rails app, it's to post a upload a file.
我有一个ajax贴子到我的后端rails应用,它是上传一个文件。
Post response with my files list in JSON
在JSON中以我的文件列表回应。
After that I need to reload my files list at view detalhes.html.erb page. This list is a partial. Here is what I have:
之后,我需要在view detalhes.html上重新加载文件列表。erb页面。这个列表是部分的。以下是我的资料:
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
My ajax post method at _arquivos_upload.html.erb
:
我的ajax post方法在_arquivos_upload.html.erb:
$.ajax({
url: 'some_url,
type: 'post', processData: false
})
.success(function(data) {
console.log(data.files.length);
// trying to reload partial after post request
$('#display-arquivos').html("<%= escape_javascript(render "arquivos_buscados") %>");
});
data.files are correct, I just need to find a way to pass this to my render partial. How can I do that? Or if this is a bad way to do it, how can I do better?
数据。文件是正确的,我只需要找到一种方法将它传递到渲染部分。我怎么做呢?或者如果这是一个不好的方法,我怎么做得更好?
Here is where I render my partial detalhes.html.erb
:
这里是我渲染部分detalhese.html。erb:
<div class="card" id="display-arquivos">
<%= render "arquivos_buscados" %>
</div>
I already have try a lot of this:
我已经试过很多了:
github链接
设置. html(data.files)
使用js。erb文件逻辑
2 个解决方案
#1
1
That looks good. I'd suggest modifying your partial to accept a local variable and then explicitly passing it in when you render the partial.
看起来不错。我建议修改局部变量以接受局部变量,然后在呈现局部变量时显式地传递它。
<div class="card" id="display-arquivos">
<%= render "arquivos_buscados", locals { files: @files #add this } %>
</div>
$('#display-arquivos').html("<%= escape_javascript(render "arquivos_buscados", locals: { files : data.files } ) %>");
$(" # display-arquivos”)。html(“<%= escape_javascript(呈现“arquivos_buscados”,局部:{files: data)。文件})% >”);
#2
1
I think I would render back html
instead of json
. Then, in .success
function of the ajax
call, just do:
我想我应该返回html而不是json。然后,在ajax调用的.success函数中,只需:
$.ajax({
url: 'some_url,
type: 'post', processData: false
})
.success(function(data) {
$('#display-arquivos').html(data);
});
You'll probably need to change your controller action along the lines of:
您可能需要按照以下步骤更改控制器操作:
def upload_arquivo
...
if @arquivo.save!
ArquivoPontoVenda.create(arquivo_id: @arquivo.id, ponto_venda_id: @ponto_venda.id, fachada: false)
@files = #populate your @files variable
render "arquivos_buscados"
else
render :something_else
end
end
#1
1
That looks good. I'd suggest modifying your partial to accept a local variable and then explicitly passing it in when you render the partial.
看起来不错。我建议修改局部变量以接受局部变量,然后在呈现局部变量时显式地传递它。
<div class="card" id="display-arquivos">
<%= render "arquivos_buscados", locals { files: @files #add this } %>
</div>
$('#display-arquivos').html("<%= escape_javascript(render "arquivos_buscados", locals: { files : data.files } ) %>");
$(" # display-arquivos”)。html(“<%= escape_javascript(呈现“arquivos_buscados”,局部:{files: data)。文件})% >”);
#2
1
I think I would render back html
instead of json
. Then, in .success
function of the ajax
call, just do:
我想我应该返回html而不是json。然后,在ajax调用的.success函数中,只需:
$.ajax({
url: 'some_url,
type: 'post', processData: false
})
.success(function(data) {
$('#display-arquivos').html(data);
});
You'll probably need to change your controller action along the lines of:
您可能需要按照以下步骤更改控制器操作:
def upload_arquivo
...
if @arquivo.save!
ArquivoPontoVenda.create(arquivo_id: @arquivo.id, ponto_venda_id: @ponto_venda.id, fachada: false)
@files = #populate your @files variable
render "arquivos_buscados"
else
render :something_else
end
end