简体   繁体   English

RoR 和 Ajax:如何使我的 Ajax 请求工作?

[英]RoR and Ajax: How can I make my Ajax request work?

I'm tying to do an Ajax request on a delete method for active storage files so my page won't reload.我想对活动存储文件的删除方法发出 Ajax 请求,这样我的页面就不会重新加载。

I have two controllers: 'project_steps' (i'm using wicked gem) and 'projects'.我有两个控制器:“project_steps”(我正在使用 wicked gem)和“projects”。

My view: project_steps/fourth_step.html.erb我的观点:project_steps/fourth_step.html.erb

<% if @project.supporting_docs.attached? %>
  <div id="remove_file">
    <%= render partial: "existing_files", :locals => {project: @project} %>
  </div>
<% end %>

My partial: project_steps/_existing_files.html.erb我的部分:project_steps/_existing_files.html.erb

<% @project.supporting_docs.each do |file| %>
  blah blah
  <%= link_to 'Remove', delete_file_attachment_project_url(file.signed_id),
     method: :delete, remote: true, class: "btn btn-sm btn-danger" %>
<% end %>

My projects_controller:我的项目_控制器:

def delete_file_attachment
     file = ActiveStorage::Blob.find_signed(params[:id])
     file.attachments.first.purge
     respond_to do |format|
       format.js
     end
   end

projects/delete_file_attachment.js.erb:项目/delete_file_attachment.js.erb:

$('#remove_file').html("<%= j render(partial: 'project_steps/existing_files', :locals => 
     {project: @project}) %>")

My Routes:我的路线:

resources :projects do
  member do
    delete :delete_file_attachment
  end
end

scope 'projects/:project_id' do
  resources :project_steps
end

My Error我的错误

ActionView::Template::Error (undefined method `supporting_docs' for nil:NilClass):
    3:     <strong>You have attached the following files:</strong>
    4:   </div>
    5:   <br>
    6:   <% @project.supporting_docs.each do |file| %>
    7:     <div class="row">
    8:       <div class="col">

My delete works fine and I see why the error is there but i'm wondering how can I make Ajax work and what i'm doing wrong?我的删除工作正常,我明白为什么会出现错误,但我想知道如何让 Ajax 工作以及我做错了什么? Happy to provide as much code as needed.很高兴根据需要提供尽可能多的代码。 Ty.泰。

PS if anyone would like to suggest a solution other than going through partial you feel might be better by all means! PS如果有人想提出一个解决方案而不是通过部分你觉得可能会更好!

When using partials with locals, you access the variable without the @ , ie to access the variable in project_steps/_existing_files.html.erb you need to use project instead of @project :当使用局部变量和局部变量时,您可以访问不带@的变量,即访问project_steps/_existing_files.html.erb中的变量,您需要使用project而不是@project

project_steps/_existing_files.html.erb project_steps/_existing_files.html.erb

<% project.supporting_docs.each do |file| %>
  blah blah
  <%= link_to 'Remove', delete_file_attachment_project_url(file.signed_id),
     method: :delete, remote: true, class: "btn btn-sm btn-danger" %>
<% end %>

Please also note that using @project in your projects/delete_file_attachment.js.erb doesn't work if you don't set the @project variable in the controller action delete_file_attachment , ie you'd probably need to add a line along @project = Project.find... to your delete_file_attachment method.另请注意,如果您未在 controller 操作delete_file_attachment中设置@project变量,则在您的projects/delete_file_attachment.js.erb中使用@project将不起作用,即您可能需要沿@project = Project.find...添加一行@project = Project.find...到您的delete_file_attachment方法。

See the Layouts and Rendering in Rails guide for more informations about using locals in partials.有关在局部中使用局部变量的更多信息,请参阅Rails 中的布局和渲染指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM