簡體   English   中英

Ruby on Rails image_tag不顯示圖像

[英]Ruby on Rails image_tag not displaying the image

在gemfile中,我安裝了carrierwave,以允許用戶上傳個人資料圖像。 我按照此railscast中的說明進行了一些操作-http : //railscasts.com/episodes/253-carrierwave-file-uploads

gem 'carrierwave'

但是,圖像未與image_tag一起顯示。 我可以通過在沒有image_tag的情況下對其進行測試來查看圖像目錄。 我還瀏覽了圖像目錄以確保存儲了圖像。 我可以在導演“ / uploads / user / image / 110 /”中看到它

在此處輸入圖片說明

在視圖中,它顯示圖像網址“ /uploads/user/image/110/thumb_IMG_0017.JPG”

<%= @user.image_url(:thumb).to_s %>

添加圖片標簽不會顯示任何內容

<%= image_tag @user.image_url(:thumb).to_s %>

圖像上傳器

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  #include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file

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

  version :thumb do
    process :resize_to_limit => [200, 200]
  end


end

控制者

def show
    @user = User.find(params[:id])
    ...
end

private

    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation, :image)
    end

模型

class User < ActiveRecord::Base
    ...

    mount_uploader :image, ImageUploader

    ...
end

例如,直接查詢用戶110的數據庫將顯示以下內容

User.find(110).image_url(:thumb)
User Load (2.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 110]]
 => "/uploads/user/image/110/thumb_IMG_0017.JPG" 

設置載波后,需要重新啟動服務器。 這將解決所有問題。

好的,我檢查了服務器日志

Checked the server log looks like a routing issue - Started GET "/uploads/user/image/111/thumb_IMG_1355.JPG" for 127.0.0.1 at 2014-07-12 11:00:29 +0100

ActionController::RoutingError (No route matches [GET] "/uploads/user/image/111/thumb_IMG_1355.JPG"):

通過進入config / environments / production.rb和development.rb並將以下內容設置為true,似乎可以解決該問題!

config.serve_static_assets = true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM