我正在做一个需要附件的项目。具体来说,我需要将图片附加到产品模型上。
也就是说,我遵循了 railscast(链接在这里)并且遵循了大部分说明(因为我认为有些说明已经过时)。我最终得到以下结果:
在产品表格中:
<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
并查看生成的附件,在产品显示视图中:
<p>
<b>Pic:</b>
<%= image_tag @product.pic.url %>
</p>
现在,问题是即使日志显示使用 选择的图像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)
我从来没有真正看到过这张照片,因为它总是说“失踪”。
现在,查看 public/system 目录,我可以看到那里的模型目录,以及 Paperclip 存储目录树,以及我试图保存的图像:
$ 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”