回形针显示丢失的图像而不是定义的图像
Paperclip shows missing image instead of defined
显示所有用户的照片有效,但显示一个用户的特定照片无效(显示丢失的图像而不是通过 ID 定义的图像)。获取特定图像的路径是 /users/:user_id/photos/:id
。这是我的 Photos 控制器:
class PhotosController < ApplicationController
before_action :find_user, only: [:index, :new, :create, :update]
before_action :find_photo, only: [:show, :destroy]
def index
@photos = @user.photos
end
def new
@photo = @user.photos.build
end
def show
end
def create
@photo = Photo.new(photo_params)
if @photo.save
if params[:images]
params[:images].each { |image|
@user.photos.create(image: image)
}
end
redirect_to :action => :index
else
render 'new'
end
end
def update
@photo = @user.photos.find(params[:id])
if @photo.update
redirect_to user_photos
else
render 'edit'
end
end
def destroy
@photo.destroy
redirect_to user_photos
end
private
def find_user
@user = User.find(params[:user_id])
end
def find_photo
@photo = Photo.find(params[:id])
end
def photo_params
params.require(:photo).permit(:title, :image, :user_id)
end
end
这是我的用户控制器:
class UsersController < ApplicationController
def index
@search = User.search(params[:q])
if @search
@users = @search.result.paginate(:per_page => 10, :page => params[:strana])
else
@users = User.all.paginate(:per_page => 10, :page => params[:strana]).order("created_at DESC")
end
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user
else
render 'new'
end
end
def show
find_params
@photos = @user.photos
end
def edit
find_params
end
def update
find_params
if @user.update(user_params)
redirect_to @user
else
render 'edit'
end
end
def destroy
find_params
@user.destroy
redirect_to users_path
end
这是photos/show视图:
<%= image_tag @photo.image.url(:thumb) %>
这是photos/index视图:
<% @user.photos.each do |photo| %>
<%= image_tag(photo.image) %>
<%= photo.title %>
<% end %>
路由是嵌套的:
resources :users do
resources :photos
end
问题已解决 - 第一张图片实际上并未上传(我忘记添加验证),因此它们在图片 link 字段中的值为 nil。
显示所有用户的照片有效,但显示一个用户的特定照片无效(显示丢失的图像而不是通过 ID 定义的图像)。获取特定图像的路径是 /users/:user_id/photos/:id
。这是我的 Photos 控制器:
class PhotosController < ApplicationController
before_action :find_user, only: [:index, :new, :create, :update]
before_action :find_photo, only: [:show, :destroy]
def index
@photos = @user.photos
end
def new
@photo = @user.photos.build
end
def show
end
def create
@photo = Photo.new(photo_params)
if @photo.save
if params[:images]
params[:images].each { |image|
@user.photos.create(image: image)
}
end
redirect_to :action => :index
else
render 'new'
end
end
def update
@photo = @user.photos.find(params[:id])
if @photo.update
redirect_to user_photos
else
render 'edit'
end
end
def destroy
@photo.destroy
redirect_to user_photos
end
private
def find_user
@user = User.find(params[:user_id])
end
def find_photo
@photo = Photo.find(params[:id])
end
def photo_params
params.require(:photo).permit(:title, :image, :user_id)
end
end
这是我的用户控制器:
class UsersController < ApplicationController
def index
@search = User.search(params[:q])
if @search
@users = @search.result.paginate(:per_page => 10, :page => params[:strana])
else
@users = User.all.paginate(:per_page => 10, :page => params[:strana]).order("created_at DESC")
end
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user
else
render 'new'
end
end
def show
find_params
@photos = @user.photos
end
def edit
find_params
end
def update
find_params
if @user.update(user_params)
redirect_to @user
else
render 'edit'
end
end
def destroy
find_params
@user.destroy
redirect_to users_path
end
这是photos/show视图:
<%= image_tag @photo.image.url(:thumb) %>
这是photos/index视图:
<% @user.photos.each do |photo| %>
<%= image_tag(photo.image) %>
<%= photo.title %>
<% end %>
路由是嵌套的:
resources :users do
resources :photos
end
问题已解决 - 第一张图片实际上并未上传(我忘记添加验证),因此它们在图片 link 字段中的值为 nil。