即使没有指定文件,以嵌套形式上传的 Carrierwave 图像仍然会创建记录
我在嵌套的 simple_form 中上传了一个 Carrierwave 图像,除非用户未指定文件,否则它会起作用(在某种程度上),在这种情况下,会创建一个空白的 Picture 对象,除非之前存在一个。不太确定如何做到这一点,以便如果用户没有指定要上传的“新”图像,则不会删除旧图像和/或创建没有文件的空白记录。
我正在做的一件事(也许很奇怪)总是将登录的@user发送到user#edit操作,然后构建一个@user.picture(如果它不存在)。我想这就是我糟糕的设计所在。
# user.rb
class User < ActiveRecord::Base
[...]
has_one :picture, :dependent => :destroy
accepts_nested_attributes_for :picture
[...]
end
# picture.rb
class Picture < ActiveRecord::Base
attr_accessible :image, :remove_image
belongs_to :user
mount_uploader :image, ImageUploader
end
# users_controller.rb
def edit
if @user.picture.nil?
@user.build_picture
end
end
#_form.html.erb
<%= simple_form_for @user, :html => {:multipart => true} do |f| %>
<%= render "shared/error_messages", :target => @user %>
<h2>Picture</h2>
<%= f.simple_fields_for :picture do |pic| %>
<% if @user.picture.image? %>
<%= image_tag @user.picture.image_url(:thumb).to_s %>
<%= pic.input :remove_image, :label => "Remove", :as => :boolean %>
<% end %>
<%= pic.input :image, :as => :file, :label => "Picture" %>
<%= pic.input :image_cache, :as => :hidden %>
<% end %>
<br/>
#rest of form here
<% end %>
I have a Carrierwave image upload in a nested simple_form which works (sort of) unless the user does not specify a file, in which case a blank Picture object is created unless there was a previously existing one. Not quite sure how to make it so that if the user doesn't specify a "new" image to upload, the old one isn't deleted and/or a blank record without a file is created.
One (maybe odd) thing I am doing is always sending the logged in @user to the user#edit action, then building a @user.picture if it doesn't exist. I am thinking this is where my bad design is.
# user.rb
class User < ActiveRecord::Base
[...]
has_one :picture, :dependent => :destroy
accepts_nested_attributes_for :picture
[...]
end
# picture.rb
class Picture < ActiveRecord::Base
attr_accessible :image, :remove_image
belongs_to :user
mount_uploader :image, ImageUploader
end
# users_controller.rb
def edit
if @user.picture.nil?
@user.build_picture
end
end
#_form.html.erb
<%= simple_form_for @user, :html => {:multipart => true} do |f| %>
<%= render "shared/error_messages", :target => @user %>
<h2>Picture</h2>
<%= f.simple_fields_for :picture do |pic| %>
<% if @user.picture.image? %>
<%= image_tag @user.picture.image_url(:thumb).to_s %>
<%= pic.input :remove_image, :label => "Remove", :as => :boolean %>
<% end %>
<%= pic.input :image, :as => :file, :label => "Picture" %>
<%= pic.input :image_cache, :as => :hidden %>
<% end %>
<br/>
#rest of form here
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我也遇到了同样的问题,我通过向accepts_nested_attribute 添加reject_if 选项来解决这个问题。所以在你的例子中,你可以做类似的事情
I think I had the same issue which I solved by adding a reject_if option to the accepts_nested_attribute. So in your example, you could do something like
当您使用 build_* 时,它会在对象上设置外键。 (类似于 Picture.new(:user_id => id) )
试试这个
When you use build_* it sets the foreign key on the object. ( similar to saying Picture.new(:user_id => id) )
Try This
今天我遇到了同样的问题,我解决了这个问题:
Today I had the same problem, I solved this like: