【发布时间】:2015-12-11 19:55:24
【问题描述】:
我正在 localhost 中测试一个使用 Paperclip 的 Rails 4 应用程序,即使提交图像会在数据库中创建条目,但图像文件夹始终是空的。
型号
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。
这是为什么?
更新
- 我使用的是 Ubuntu 14.04。
- Imagemagick 已安装。
【问题讨论】:
-
请显示您的控制器创建/更新操作和表单视图
-
嗨,如果你只是按照这个,你会没事的:github.com/thoughtbot/paperclip
-
您的视图可能看起来像这样:
-
我添加了控制器
-
能否也为表单添加视图?
标签: ruby-on-rails ruby-on-rails-4 paperclip