即使没有指定文件,以嵌套形式上传的 Carrierwave 图像仍然会创建记录

发布于 2024-12-07 17:27:27 字数 1479 浏览 1 评论 0原文

我在嵌套的 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-12-14 17:27:27

我想我也遇到了同样的问题,我通过向accepts_nested_attribute 添加reject_if 选项来解决这个问题。所以在你的例子中,你可以做类似的事情

class User < ActiveRecord::Base
[...]

  has_one :picture, :dependent => :destroy
  accepts_nested_attributes_for :picture,
    :reject_if => lambda { |p| p.image.blank? }

[...]
end

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

class User < ActiveRecord::Base
[...]

  has_one :picture, :dependent => :destroy
  accepts_nested_attributes_for :picture,
    :reject_if => lambda { |p| p.image.blank? }

[...]
end
你的往事 2024-12-14 17:27:27

当您使用 build_* 时,它会在对象上设置外键。 (类似于 Picture.new(:user_id => id) )

试试这个

# users_controller.rb
def edit
  if @user.picture.nil?
    @user.picture = Picture.new
  end
end

When you use build_* it sets the foreign key on the object. ( similar to saying Picture.new(:user_id => id) )

Try This

# users_controller.rb
def edit
  if @user.picture.nil?
    @user.picture = Picture.new
  end
end
无尽的现实 2024-12-14 17:27:27

今天我遇到了同样的问题,我解决了这个问题:

  accepts_nested_attributes_for :photos,
    :reject_if => :all_blank

Today I had the same problem, I solved this like:

  accepts_nested_attributes_for :photos,
    :reject_if => :all_blank
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文