我在localhost中测试使用Paperclip的Rails 4应用程序,即使提交图像在数据库中创建条目,images文件夹也总是空的。
模型
class Tile < ActiveRecord::Base
belongs_to :game
# Paperclip
has_attached_file :image,
styles: { medium: "300x300>", thumb: "100x100>" },
url: "/images/:style/:filename",
default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
控制器
class TilesController < ApplicationController
before_action :set_game
def create
tile = @game.tiles.create tile_params
redirect_to @game, notice: tile
end
private
# Use callbacks to share common setup or constraints between actions.
def set_game
@game = Game.find(params[:game_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tile_params
params.require(:tile).permit(:image)
end
end
查看表单
<%= form_for([@game, Tile.new], multipart: true) do |form| %>
<%= form.file_field :image %>
<% end %>
每当我尝试打印<p><%= tile.image.url %></p>
时,我都会/images/original/missing.png
。
为什么会这样?
更新
答案 0 :(得分:0)
请务必同时使用path
更新url
,即
has_attached_file :image,
styles: { medium: "300x300>", thumb: "100x100>" },
path: "/images/:style/:filename",
url: "/images/:style/:filename",
default_url: "/images/:style/missing.png"
正如this Railscasts~5:20中所述,url
设置了从中检索图像的位置,path
设置了它们的存储位置。
答案 1 :(得分:0)
假设您正在使用window
,则在屏幕上显示图片:image_tag(tile.image.url(:small))
另外,您安装了ImageMagick and file for Windown?'
如果是,您是否在Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
中将环境设置为:config/environments/development.rb
?然后重启你的服务器。