2013-04-22 76 views
1

我正在开发一个需要附件的项目。具体来说,我需要将图片附加到产品模型。回形针保存文件但不能读取它们

这就是说,我遵循railscast(链接here),我遵循大部分指示(因为我认为有些过时)。我结束了以下内容:

在产品形式

<div class="field"> 
    <%= f.label "Pic"%><br /> 
    <%= f.file_field :pic %> 
    </div> 

在产品模式

class Product < ActiveRecord::Base 
    attr_accessible :brand_id, :name, :pic 

    attr_accessor :pic_file_name 
    attr_accessor :pic_content_type 
    attr_accessor :pic_file_size 
    attr_accessor :pic_updated_at 

    has_attached_file :pic 

而且看到导致附件,在产品显示查看:

现在10

,事情是,即使日志显示,随着file_field选择的图像被成功上传:

Started POST "/products" for 127.0.0.1 at 2013-04-22 17:10:56 -0500 
Processing by ProductsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"llIJTXKH87e7vpjX1A7Kmcz6BD2UToPvIfVd1bqmA58=", "product"=>{"brand_id"=>"1", "name"=>"asda\ 
sdsa", "pic"=>#<ActionDispatch::Http::UploadedFile:0x007f9cfdb201c8 @original_filename="3.JPG", @content_type="image/jpeg", @headers="Conten\ 
t-Disposition: form-data; name=\"product[pic]\"; filename=\"3.JPG\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/_r/d96rh\ 
0652xndcknb2txzn77r0000gn/T/RackMultipart20130422-2306-qoojw2>>}, "commit"=>"Create Product"} 
    ^[[1m^[[36m (0.1ms)^[[0m ^[[1mbegin transaction^[[0m 
    ^[[1m^[[35mSQL (76.1ms)^[[0m INSERT INTO "products" ("brand_id", "created_at", "image_content_type", "image_file_name", "image_file_size"\ 
, "image_updated_at", "name", "pic_content_type", "pic_file_name", "pic_file_size", "pic_updated_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?\ 
, ?, ?, ?, ?, ?, ?) [["brand_id", 1], ["created_at", Mon, 22 Apr 2013 22:10:56 UTC +00:00], ["image_content_type", nil], ["image_file_name"\ 
, nil], ["image_file_size", nil], ["image_updated_at", nil], ["name", "asdasdsa"], ["pic_content_type", nil], ["pic_file_name", nil], ["pic_\ 
file_size", nil], ["pic_updated_at", nil], ["updated_at", Mon, 22 Apr 2013 22:10:56 UTC +00:00]] 
[paperclip] Saving attachments. 
    ^[[1m^[[36m (0.9ms)^[[0m ^[[1mcommit transaction^[[0m 
Redirected to http://localhost:3000/products/10 
Completed 302 Found in 126ms (ActiveRecord: 77.1ms) 

我从来没有真正能看到的图像,因为它总是说“失踪”。现在

,寻找到公共/系统目录,我可以看到有模型目录,以及回纹针存储目录树,还有像我试图挽救:

$ ls public/system/products/pics/000/000/010/original/ 
3.JPG 

我做设置ImageMagick的路径,文件

配置/初始化/ paperclip.rb

持有:

Paperclip.options[:command_path] = "/Users/<my_user>/Documents/non-ios_apps/ImageMagick-6.8.1/bin/" 

所以,我不知道为什么图像被上传,但无法读取。就附件而言,这可能与它没有保存任何东西到数据库中有关,但我不确定它们是单独的问题还是触发另一个问题。

任何帮助表示赞赏。

谢谢!

使用: 的Rails 3.2.10 “回形针”,“〜> 3.0”

回答

1

它看起来像正在上传与发布请求的文件,但如果你在看SQL脚本运行,所有的图片属性都设置为零。

我不确定模型中的attr_accessor行来自哪里,因为我没有看到它们在railscast或当前回形针文档中提及。这些可能是导致你的问题的原因。

railscast已经有五年了,所以我建议去github上的回形针项目https://github.com/thoughtbot/paperclip,阅读如何安装和使用当前版本的回形针。他们有一个非常简单的演练来启动和运行。

+0

谢谢!由于设置时出现错误,我添加了所有attr_accessor,这是我过去遇到过的大量任务,而且我认为如果回形针移植不完整或者类似情况,我需要添加这些。 – snowingheart 2013-04-22 23:50:47