Rails 5 carrierwave没有路线与图片相符 [英] rails 5 carrierwave no route matches for image
问题描述
我需要一些帮助。我正在使用Rails 5和carrierwave上传图像。我遇到的问题是图像已保存但无法显示,我找不到与GET匹配的路线...。我将图像存储在Rails.root的上载文件夹中,因为我不想将其公开保存
I need some help. I am using rails 5 and carrierwave to upload images. the issue I am having is the images are saved but will not display, I get a no route matches GET..... I am storing the images in a uploads folder at Rails.root since I do not want them saved in the public dir.
image_uploader.rb
image_uploader.rb
def store_dir
"uploads/#{model.user_id}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
carrier_wave.rb
carrier_wave.rb
require 'carrierwave/orm/activerecord'
require 'carrierwave'
CarrierWave.configure do |config|
config.root = Rails.root
end
这是错误我正在获取日志:
This is the error that I am getting in the logs:
Started GET "/uploads/2/uitem/image/26/A_penrose-small.jpg" for ::1 at 2016-10-29 16:06:27 +0200
ActionController::RoutingError (No route matches [GET] "/uploads/2/uitem/image/26/A_penrose-small.jpg"):
任何帮助将不胜感激,我一直在搜索,并且找不到解决方案。
谢谢
Any Help would be greatly appreciated, I have been searching all over and cannot find a solution. Thank you
推荐答案
找不到图片的原因是Web服务器的根目录是Rails应用程序的公共目录目录,因此它将在 public / uploads / 2 / ...下查找图像。
The reason the image isn't being found is the web server's root is the Rails app's public directory, so it is looking for the image under "public/uploads/2/..."
由于Web服务器找不到文件,因此它正在传递请求到Rails堆栈,并且没有定义匹配的路由,因此会引发错误。
Since the web server can't find the file it is passing the request to the Rails stack, and there is no route defined that matches, so it throws an error.
如果您将图片上传根目录更改为公共目录,则应该
If you change your image upload root to be the public directory it should work as you expect.
如果您不想将图像放在公共目录下您可以创建路线和控制器操作来投放图像。您可以在控制器操作中使用send_file方法。看看这个问题:
If you don't want to put the images under the public directory you can create a route and controller action to serve the images. You can use the send_file method in the controller action. Take a look at this question:
Rails 3, displaying jpg images from the local file system above RAILS_ROOT
这篇关于Rails 5 carrierwave没有路线与图片相符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!