首页 > 解决方案 > Ruby On Rails Active Storage 清除/删除方法抛出 NoMethodError

问题描述

在通过 Active Storage 上传图像后,我的图像清除/删除有问题。我已尝试按照说明进行操作,但仍然遇到相同的错误。该错误在命令行中描述为 Template Error ::Error (undefined method 'stringify_keys' for ....

任何建议都会很棒

在此处输入图像描述

命令行

ActionView::Template::Error (undefined method `stringify_keys' for "http://localhost:3000/jobs/21/delete_image_attachment":String):
    11:         <div class="panel-body">
    12:           <span class="pull-right">
    13:             <%=
    14:             link_to 'Remove', delete_image_attachment_job_url(@job.images[image]),
    15:             remote: true,
    16:             method: :delete,
    17:             data: {confirm: "Are you sure"} do %>

app/views/jobs/_photos_list.html.erb:14:in `block in _app_views_jobs__photos_list_html_erb__192211306_83310024'
app/views/jobs/_photos_list.html.erb:5:in `_app_views_jobs__photos_list_html_erb__192211306_83310024'
app/views/jobs/photo_upload.html.erb:51:in `_app_views_jobs_photo_upload_html_erb___795227943_83576688'

我的模特

class Job < ApplicationRecord
  belongs_to :user
  has_many_attached :images

  validates :job_category, presence: true
  validates :job_type, presence: true
  validates :recurrence, presence: true
  #validates :job_title, presence: true
  #validates :job_description, presence: true

end

控制器

class JobsController < ApplicationController

  before_action :set_job , except: [:index, :new, :create, :edit]
  before_action :authenticate_user!, except: [:show]
  before_action :is_authorised, only: [:listing, :budget, :description, :photo_upload, :location, :update, :show, :delete ]

  def delete_image_attachment
    @images = ActiveStorage::Attachment.find(params[:id])
    @images.purge
    redirect_back(fallback_location: request.referer)
  end

风景

<% if @job.images.attached? %>
  <br/>

  <div class="row">
    <% (0...@job.images.count).each do |image| %>
    <div class="col-md-4">
      <div class="panel panel-default">
        <div class="panel-heading preview">
          <%= image_tag(@job.images[image]) %>
        </div>
        <div class="panel-body">
          <span class="pull-right">
            <%=
            link_to 'Remove', delete_image_attachment_job_url(@job.images[image]),
            remote: true,
            method: :delete,
            data: {confirm: "Are you sure"} do %>
              <i class="fa fa-trash-o" aria-hidden="true"></i>
            <% end %>
          </span>
        </div>
      </div>
    </div>
    <% end %>
  </div>
<% end %>

我的路线

Rails.application.routes.draw do

  root 'pages#home'

  resources :jobs_devs
  resources :jobs

  resources :jobs_devs, except: [:placeholder] do
    member do
      get 'listing'
      get 'budget'
      get 'description'
      get 'photo_upload'
      get 'featured'
      get 'location'
      get 'update'
      get 'premium'
      get 'show'
      delete :delete_image_attachment
    end
  end

  devise_for    :users,
            :path => '',
            :path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile', :sign_up => 'registration'},
            :controllers => {:omniauth_callbacks => 'omniauth_callbacks', :registrations => 'registrations' }

  resources :users, only: [:show]
  resources :jobs, except: [:edit] do
    member do
      get 'listing'
      get 'budget'
      get 'description'
      get 'photo_upload'
      get 'featured'
      get 'location'
      get 'update'
      get 'premium'
      get 'show'
      delete :delete_image_attachment
    end
  end
end

标签: ruby-on-railsruby

解决方案


因为您使用的是块形式,所以link_to应该将文本放在 do/end 之间:

        <%=
        link_to delete_image_attachment_job_url(@job.images[image]),
        remote: true,
        method: :delete,
        data: {confirm: "Are you sure"} do %>
          <i class="fa fa-trash-o" aria-hidden="true">Remove</i>
        <% end %>

就像文档中的示例一样:

如果您的链接目标难以适应名称参数,您也可以使用块。ERB 示例:

<%= link_to(@profile) do %>
  <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
<% end %>
# => <a href="/profiles/1">
       <strong>David</strong> -- <span>Check it out!</span>
     </a>

推荐阅读