Rails:ActiveRecord :: RecordNotFound在没有ID的情况下找不到照片

时间:2018-09-28 12:33:35

标签: ruby-on-rails paperclip rails-activestorage

我有两个模型,服务和照片。在服务模型下,belongs_to:user和has_many:photos和has_many_attached:images。

在照片模型属下属于:service

在“照片”查看器下,我试图显示与每个服务关联的图像。

当我尝试删除图像时,出现ID错误的找不到图像。

查看器如下所示:

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

<div class="row">
  <% @service.images.each do |image| %>
    <div class="col-md-4">
      <div class="panel panel-default">
        <div class="panel-heading preview">
          <%= image_tag image %>
        </div>
        <div class="panel-body">
          <span class="pull-right">
            <%= link_to service_photo_path(@photos, 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 %>

照片控制器如下所示:

class PhotosController < ApplicationController

  def create
    @service = Service.find(params[:service_id])

    if params[:images]
      params[:images].each do |img|
        @service.photos.create(image: img)
    end

    @photos = @service.photos
    redirect_back(fallback_location: request.referer, notice: "Saved...")
  end
end

  def destroy
    @photo = Photo.find(params[:_id])
    service = @photo.service

    @photo.destroy
    @photos = Photo.where(service_id: service_id)

    respond_to :js
  end
end

路线:

service_photos POST /services/:service_id/photos(.:format)photos#create 
service_photo DELETE /services/:service_id/photos/:id(.:format) photos#destroy

1 个答案:

答案 0 :(得分:1)

替换

@photo = Photo.find(params[:_id])

具有:

@photo = Photo.find(params[:id])

我认为您有错字! id不是_id


此外,在您看来,请替换:

service_photo_path(@photos, image)

具有:

service_photo_path(@service, photo)