Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 回形针&x2B;rails 4.2-图像文件名为空_Ruby On Rails_Ruby_Amazon Web Services_Amazon S3_Paperclip - Fatal编程技术网

Ruby on rails 回形针&x2B;rails 4.2-图像文件名为空

Ruby on rails 回形针&x2B;rails 4.2-图像文件名为空,ruby-on-rails,ruby,amazon-web-services,amazon-s3,paperclip,Ruby On Rails,Ruby,Amazon Web Services,Amazon S3,Paperclip,我已经在Rails 4.2.1应用程序上安装了回形针(v4.2.1)+aws-sdk-v1gem。模型设置如下: 型号: class Photo < ActiveRecord::Base attr_accessor :image_file_name has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url =&g

我已经在Rails 4.2.1应用程序上安装了
回形针
(v4.2.1)+
aws-sdk-v1
gem。模型设置如下:

型号:

class Photo < ActiveRecord::Base

  attr_accessor :image_file_name
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

end
 = form_for @photo_set_new_photo, :html => { :multipart => true } do |f|
    = f.file_field(:image, {class: 'photo-upload', accept:"image/*;capture=camera"})
def create
  @photo = Photo.new(photo_params)

  respond_to do |format|
    if @photo.save
      format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
      format.json { render :show, status: :created, location: @photo }
    else
      format.html { render :new }
      format.json { render json: @photo.errors, status: :unprocessable_entity }
    end
  end
end

def photo_params
  params.require(:photo).permit(:category_id, :photo_set_id, :image)
end
控制器:

class Photo < ActiveRecord::Base

  attr_accessor :image_file_name
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

end
 = form_for @photo_set_new_photo, :html => { :multipart => true } do |f|
    = f.file_field(:image, {class: 'photo-upload', accept:"image/*;capture=camera"})
def create
  @photo = Photo.new(photo_params)

  respond_to do |format|
    if @photo.save
      format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
      format.json { render :show, status: :created, location: @photo }
    else
      format.html { render :new }
      format.json { render json: @photo.errors, status: :unprocessable_entity }
    end
  end
end

def photo_params
  params.require(:photo).permit(:category_id, :photo_set_id, :image)
end
这就是你所期望的标准饼干切割器。提交此表格时,我的日志中有:

Started POST "/photos" for 127.0.0.1 at 2015-05-04 11:47:04 +0200
Processing by PhotosController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "photo"=>{"category_id"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x007ffe5c421ed8 @tempfile=#<Tempfile:/tmp/RackMultipart20150504-20205-c0g1d6.png>, @original_filename="antaeater-birthday.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"antaeater-birthday.png\"\r\nContent-Type: image/png\r\n">}}
Command :: file -b --mime '/tmp/3bdad8b82d38ab9d77cab8cd85c2877120150504-20205-1li6xz7.png'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3bdad8b82d38ab9d77cab8cd85c2877120150504-20205-2uc2d6.png[0]' 2>/dev/null
Command :: identify -format %m '/tmp/3bdad8b82d38ab9d77cab8cd85c2877120150504-20205-2uc2d6.png[0]'
Command :: convert '/tmp/3bdad8b82d38ab9d77cab8cd85c2877120150504-20205-2uc2d6.png[0]' -auto-orient -resize "300x300>" '/tmp/6025e18d9f999e893c1840772ad7c87b20150504-20205-v780mg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3bdad8b82d38ab9d77cab8cd85c2877120150504-20205-2uc2d6.png[0]' 2>/dev/null
Command :: identify -format %m '/tmp/3bdad8b82d38ab9d77cab8cd85c2877120150504-20205-2uc2d6.png[0]'
Command :: convert '/tmp/3bdad8b82d38ab9d77cab8cd85c2877120150504-20205-2uc2d6.png[0]' -auto-orient -resize "100x100>" '/tmp/6025e18d9f999e893c1840772ad7c87b20150504-20205-1qlvsdh'
   (0.2ms)  BEGIN
Command :: file -b --mime '/tmp/3bdad8b82d38ab9d77cab8cd85c2877120150504-20205-3p5x8o.png'
  SQL (0.3ms)  INSERT INTO "photos" ("image_content_type", "image_file_size", "image_updated_at", "image_file_name", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["image_content_type", "image/png"], ["image_file_size", 1241569], ["image_updated_at", "2015-05-04 09:47:04.866533"], ["image_file_name", nil], ["created_at", "2015-05-04 09:47:05.173238"], ["updated_at", "2015-05-04 09:47:05.173238"]]
[paperclip] saving /photos/images/000/000/008/original/antaeater-birthday.png
[AWS S3 200 2.681968 0 retries] put_object(:acl=>:public_read,:bucket_name=>"bucket-development",:content_length=>1241569,:content_type=>"image/png",:data=>Paperclip::UploadedFileAdapter: antaeater-birthday.png,:key=>"photos/images/000/000/008/original/antaeater-birthday.png")  

[paperclip] saving /photos/images/000/000/008/medium/antaeater-birthday.png
[AWS S3 200 0.17464 0 retries] put_object(:acl=>:public_read,:bucket_name=>"bucket-development",:content_length=>145007,:content_type=>"image/png",:data=>Paperclip::FileAdapter: 6025e18d9f999e893c1840772ad7c87b20150504-20205-v780mg,:key=>"photos/images/000/000/008/medium/antaeater-birthday.png")  

[paperclip] saving /photos/images/000/000/008/thumb/antaeater-birthday.png
[AWS S3 200 0.074367 0 retries] put_object(:acl=>:public_read,:bucket_name=>"bucket-development",:content_length=>18256,:content_type=>"image/png",:data=>Paperclip::FileAdapter: 6025e18d9f999e893c1840772ad7c87b20150504-20205-1qlvsdh,:key=>"photos/images/000/000/008/thumb/antaeater-birthday.png")  

如您所见-图像文件名
为空
,这已经是日志告诉我们的。问题是-为什么?

我认为您明确地将
图像文件名
分配到虚拟属性中

也许
attr\u accessor:image\u file\u name
是不需要的,因为我记得
回形针将为您存储
image\u file\u name

(facepalm)好的,您很好,先生!我自己解释一下:我很确定在开发过程中,回形针曾经因为没有属性访问器而责骂我,所以我把它放了进去。