rails 5 carrierwave no route matches for image

核能气质少年 提交于 2019-12-11 03:09:59

问题


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

  def store_dir
   "uploads/#{model.user_id}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

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


回答1:


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/..."

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.

UPDATE

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




来源:https://stackoverflow.com/questions/40319938/rails-5-carrierwave-no-route-matches-for-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!