我创建了一个表单,允许用户通过两个脚手架代创建一个专辑(一个用于专辑,另一个用于照片本身)。我试图建立一个用户可以看到"删除"按钮会破坏图片。会有一个小小的警报来确认图片的破坏,然后图片会被破坏,浏览器会在成功通知的情况下重新加载页面。
我的主要问题是我试图访问所选的图片,但我认为Rails会在照片的ID和相册的ID之间混淆。
这是我的代码:
查看/ edit.html.erb
data= json_decode(file_get_contents("php://input"));
$usercomment= mysql_real_escape_string($data->usercomment);
$wardrobe= mysql_real_escape_string($data->wardrobe);
$ctype= mysql_real_escape_string($data->ctype);
$pic= mysql_real_escape_string($data->pic);
$vtype= mysql_real_escape_string($data->vtype);
$query="INSERT INTO db.comment(`comment`,`pic`,`wardrobe`,`comment_type`,`vtype`)
VALUES('".$usercomment."','".$pic."','".$wardrobe."','".$ctype."','".$vtype."')";
$Result1 = mysql_query($query) or die(mysql_error());
album_controller.rb
<h1>Editing Album</h1>
<%= render 'form', album: @album %>
<% @album.photos.each do |p| %>
<%= image_tag p.image %>
#Attempt to reach the photo and to destroy it
<%= link_to 'Destroy', @photo, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<%= link_to 'Show', @album %> |
<%= link_to 'Back', albums_path %>
photos_controller.rb
class AlbumsController < ApplicationController
before_action :set_album, only: [:show, :edit, :update, :destroy]
#...
def create
@album = Album.new(album_params)
if @album.save
# to handle multiple images upload on create
if params[:images]
params[:images].each { |image|
@album.photos.create(image: image)
}
end
redirect_to @album, notice: 'Your album has been created.'
else
render :new, alert: 'Something went wrong.'
end
end
def update
if @album.update(params[:album].permit(:title,:description))
# to handle multiple images upload on update when user add more picture
if params[:images]
params[:images].each { |image|
@album.photos.create(image: image)
}
end
redirect_to @album, notice: 'Album has been updated.'
else
render :edit
end
end
def destroy
@album.destroy
redirect_to albums_url, notice: 'Album was successfully destroyed.'
end
private
def set_album
#Attempt to select individually the album and the photos
@album = Album.find(params[:id])
@photo = Photo.find(params[:id])
end
def album_params
params.require(:album).permit(:title, :description, :user_id, :image)
end
end
模型/ photo.rb
class PhotosController < ApplicationController
before_action :set_photo, only: [:show, :edit, :update, :destroy]
# trucated for space
def update
if @photo.update(photo_params)
redirect_to @photo, notice: 'Photo was successfully updated.'
else
render :edit
end
end
def destroy
@photo.destroy
redirect_to :back, notice: 'Photo was successfully destroyed.'
end
private
def set_photo
@photo = Photo.find(params[:id])
end
def photo_params
params.require(:photo).permit(:album_id, :image)
end
end
模型/ album.rb
class Photo < ApplicationRecord
belongs_to :album
validates_presence_of :album
mount_uploader :image, ImageUploader
end
当我尝试删除图片时,这是有效的,我无法在我的&#34;上传&#34;但是,当我尝试重新加载页面时,出现以下错误:class Album < ApplicationRecord
has_many :photos
accepts_nested_attributes_for :photos, allow_destroy: true
end
,定位行Couldn't find Photo with 'id'=4
我的问题是:如何在不出现这类错误的情况下以正确的方式定位和删除图片?
提前谢谢
答案 0 :(得分:3)
问题是您在params[:id]
中使用AlbumController
作为相册的ID和照片的ID:
@album = Album.find(params[:id])
@photo = Photo.find(params[:id])
这没有任何意义,你将从中得到无意义的结果。你应该删除第二行(所以只是获取相册),并在你的视图中引用p
(来自each
循环)而不是@photo
:< / p>
<% @album.photos.each do |p| %>
<%= image_tag p.image %>
<%= link_to 'Destroy', p, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>