Ruby on rails 从远程位置上载文件和删除文件在carrierwave中不起作用
我有一个用户模型的嵌套ressource图片。如果我试图上传带有“远程文件url”的图片,它会上传carrierwave tmp目录中的图片,但不会将它们移动到实际目录中。这“应该”是一个验证问题。但事实并非如此。它在控制台中工作正常:Ruby on rails 从远程位置上载文件和删除文件在carrierwave中不起作用,ruby-on-rails,ruby-on-rails-3,file-upload,carrierwave,Ruby On Rails,Ruby On Rails 3,File Upload,Carrierwave,我有一个用户模型的嵌套ressource图片。如果我试图上传带有“远程文件url”的图片,它会上传carrierwave tmp目录中的图片,但不会将它们移动到实际目录中。这“应该”是一个验证问题。但事实并非如此。它在控制台中工作正常: p = Picture.find(360) p.remote_file_url = "http://example.com/somepic.jpg" p.save! 使用图片更新用户请求的我的参数: "user"=>{"pictures_attribu
p = Picture.find(360)
p.remote_file_url = "http://example.com/somepic.jpg"
p.save!
使用图片更新用户请求的我的参数:
"user"=>{"pictures_attributes"=>{"0"=>{"remote_file_url"=>"http://example.com/somepic.jpg", "id"=>"359"}
如果我在没有远程文件url(仅输入文件字段)的情况下上载,它可以工作:
"user"=>{"pictures_attributes"=>{"0"=> { "file"=>#<ActionDispatch::Http>, "remote_file_url"=>"", "id"=>"359"}
图片模型:
class Picture < ActiveRecord::Base
attr_accessible :file, :file_cache, :remove_file, :position, :remote_file_url
mount_uploader :file, PictureUploader
belongs_to :user
end
我有一个类似的问题,当我设置远程图像url时,图像不会与模型本身一起保存 问题是,设置远程图像url后,图片未保存 这是因为我从用户模型中调用了一个委托方法
self.taker='Andrew'
,该方法会自动将picture.taker中的'Andrew'保存到数据库中。我必须避免这种情况,并使用'self.picture.taker='Andrew'来避免图片被持久化。然后,当我调用user.save时,它将保存相关图片并正确创建缩略图
这种情况是特定的,但重点是检查您的图片对象在设置远程图像url后是否已实际保存。这是因为attr\u可访问。如果您使装载的上传程序的实例变量名attr_可访问,Ruby将为您创建访问器。。。这会干扰卡里尔瓦夫的二传手
不要让任何装载的上传程序变量名attr_可访问 风景怎么样?控制器的代码是什么?视图的实际erb是什么?我刚刚注意到“删除文件”也不起作用。我将Rails3.1与Ruby1.9.2结合使用,如果这样有帮助的话。。。
class Picture < ActiveRecord::Base
attr_accessible :file, :file_cache, :remove_file, :position, :remote_file_url
mount_uploader :file, PictureUploader
belongs_to :user
end
# encoding: UTF-8
class User < ActiveRecord::Base
attr_accessible :remote_file_url, :beta_token, :wants_gender, :gender, :radius, :email, :password, :password_confirmation, :remember_me, :region, :latitude, :longitude, :gmaps, :pictures_attributes
has_many :pictures, :dependent => :destroy
accepts_nested_attributes_for :pictures, :allow_destroy => true
end
= simple_form_for @user, :url => profile_path(@user) do |f|
%ul.profiles.users
- @user.pictures.each_with_index do |picture, i|
%li.user
.fields
= f.simple_fields_for :pictures, picture do |picture_from|
- if picture.file.present?
= picture_from.input :_destroy, :as => :boolean
= picture_from.input :position
= picture_from.input :file
= picture_from.input :remote_file_url, :as => :hidden
= picture_from.input :file_cache