assets/books_icon
我有一个 Rails 应用程序,它将在用户创建书籍时上传名称与书籍 ID 相同的图像。
upload_icon(params[:book][:image_upload])
# upload_image when create/update book
def upload_icon(uploaded_io)
photo_directory = "app/assets/images/"
# only when user upload the iphoto
if uploaded_io.present?
# upload to the exact location with category_id
extension = File.extname(uploaded_io.original_filename)
photo_location = 'books_image/'+ @book.id.to_s + extension
# open in binary mode
photo_full_location = photo_directory + photo_location
File.open(photo_full_location, 'wb') do |file|
file.write(uploaded_io.read)
end
# only have to state which is the directory,
# image_tag will use assets piepline which will add 'assets/images/' as prefix in src
@book.update_attribute(:image_url, photo_location)
end
end
它在开发模式下工作。然后,我在以mysql为数据库的mac Apache2服务器上部署了带有乘客的rails应用程序,并config.assets.compile = true
在开发配置文件中进行了更改。
在生产模式下,我可以创建新书并将图像上传到assets/books_icon
. 但是,我刚刚上传的图像的文件路径不会update
。
比如我新建了一本id为2的书,里面有2.jpg assets/books_icon
。但是rails应用程序会告诉我http://localhost/assets/images/books_icon/2.jpg
丢失了。
但是,当我重新启动 apache 服务器时,我可以在以下位置查看图片http://localhost/assets/images/books_icon/2.jpg
有什么办法可以解决这类caching
问题吗??