从数据库中删除头像

时间:2017-12-05 17:20:41

标签: ruby-on-rails ruby

我为我的用户个人资料图片创建了一个销毁操作。销毁操作正常工作,照片将被删除。我可以添加另一张图片没有任何问题,但当我尝试再次删除图片时,我收到以下错误消息:

无法使用' id' = 1

找到头像

这是我的数据库Avatar.all中存储的内容:

[#<Avatar id: 2, avatarpic: "cat_astronaut.jpg", user_id: 1, created_at: "2017-12-05 17:02:08", updated_at: "2017-12-05 17:02:08">]

id为2而不是1,即使我成功删除了id为1的第一张图片。每次删除图片并上传新图片时,id都会递增。因此,当我尝试删除第二张上传的图片时,它无法找到正确的ID。我每次删除图片时都需要将ID重置为1.

控制器如下:

class AvatarsController < ApplicationController

  def create
    @user = User.find(params[:user_id])
    @avatar = Avatar.new(avatar_params)
    @avatar.user = @user
    @avatar.save
    # Three lines above can be replaced with 
    # @user.create_avatar(params)
    redirect_to user_path(@user)
  end

def destroy
  @user = User.find(params[:user_id])
  @avatar = Avatar.find(params[:id])
  @avatar.user = @user
  @avatar.destroy
  redirect_to user_path(@user)
end

private

def avatar_params
  params.require(:avatar).permit(:avatarpic)
end

end

根据我的用户型号,只有一张照片:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :places
  has_many :comments, dependent: :destroy
  has_one :avatar, dependent: :destroy
end

用户观看个人资料页面的代码:

<%= link_to user_avatar_path(@user), :title => "Upload New Photo", method: :delete, data: { confirm: 'Are you sure you want to change your Photo?' } do %>
  <%= image_tag @user.avatar.avatarpic %>
<% end %>

rake routes:

root GET    /                                     places#index
          place_comments POST   /places/:place_id/comments(.:format)  comments#create
            place_photos POST   /places/:place_id/photos(.:format)    photos#create
                  places GET    /places(.:format)                     places#index
                         POST   /places(.:format)                     places#create
               new_place GET    /places/new(.:format)                 places#new
              edit_place GET    /places/:id/edit(.:format)            places#edit
                   place GET    /places/:id(.:format)                 places#show
                         PATCH  /places/:id(.:format)                 places#update
                         PUT    /places/:id(.:format)                 places#update
                         DELETE /places/:id(.:format)                 places#destroy
            user_avatars POST   /users/:user_id/avatars(.:format)     avatars#create
             user_avatar DELETE /users/:user_id/avatars/:id(.:format) avatars#destroy
                    user GET    /users/:id(.:format)                  users#show

routes.rb中:

Rails.application.routes.draw do
  devise_for :users
  root 'places#index'
  resources :places do
    resources :comments, only: :create
    resources :photos, only: :create
  end
  resources :users, only: [:show] do
    resources :avatars, only: [:create]
    resources :avatars, only: [:destroy]
    # or resources :avatars, only: [:create, :destroy]
end 
end

头像模型:

class Avatar < ApplicationRecord
  belongs_to :user
  mount_uploader :avatarpic, AvatarUploader
end

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您的link_to遗失了avatar_id。变化

user_avatar_path(@user)

user_avatar_path(@user, @user.avatar)