【问题标题】:RoR and Ajax: How can I make my Ajax request work?RoR 和 Ajax:如何使我的 Ajax 请求工作?
【发布时间】:2021-08-30 05:51:22
【问题描述】:

我正准备对活动存储文件的删除方法执行 Ajax 请求,因此我的页面不会重新加载。

我有两个控制器:“project_steps”(我正在使用 wicked gem)和“projects”。

我的看法:project_steps/fourth_step.html.erb

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

我的部分: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 %>

我的项目控制器:

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:

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

我的路线:

resources :projects do
  member do
    delete :delete_file_attachment
  end
end

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

我的错误

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">

我的删除工作正常,我明白为什么会出现错误,但我想知道如何让 Ajax 工作以及我做错了什么?很高兴根据需要提供尽可能多的代码!泰。

P.S 如果有人想提出一个解决方案,而不是通过部分你觉得可能会更好!

【问题讨论】:

    标签: ruby-on-rails ajax rails-activestorage ruby-on-rails-6.1


    【解决方案1】:

    当使用带局部变量的局部变量时,您可以在不使用@ 的情况下访问变量,即要访问project_steps/_existing_files.html.erb 中的变量,您需要使用project 而不是@project

    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 %>
    

    还请注意,如果您没有在控制器操作delete_file_attachment 中设置@project 变量,则在您的projects/delete_file_attachment.js.erb 中使用@project 将不起作用,即您可能需要添加一行@project = Project.find ... 到您的 delete_file_attachment 方法。

    请参阅Layouts and Rendering in Rails 指南,了解有关在局部变量中使用局部变量的更多信息。

    【讨论】:

    • 谢谢!我知道我很接近,但是当我尝试在操作或我的 before_action 中定义 @project = Project.find(params[:id]) 时,它会尝试将项目 id 设置为 blob signed_id? ActiveRecord::RecordNotFound(找不到 'id'=eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcE1CIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6e3554cd4999501042c32eba11dac2ded007c63a 的项目)
    • 因为您在 delete_file_attachment_project_url(file.signed_id) 方法中传入的内容会在您的视图中调用。您的应用程序需要 URL 帮助程序中的项目 ID,因为您的 routes.rb 文件中的定义会创建一个如下 URL: /projects/:id/delete_file_attachment 其中 :id 参数是项目的 ID。您可以尝试: delete_file_attachment_project_url(project.id, file: file.signed_id) 这将生成一个类似的 URL: /projects//delete_file_attachment?file= 并且您可以访问这两个 ID。或者重新考虑你的路线。
    猜你喜欢
    • 2012-02-01
    • 2016-01-07
    • 2012-05-10
    • 1970-01-01
    • 2015-02-02
    • 2014-03-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多