【问题标题】:Add multiple images with cocoon gem使用茧宝石添加多个图像
【发布时间】:2015-12-17 23:27:58
【问题描述】:

首先,我不得不说我是 Rails 新手,很抱歉提出新手问题。 我有一个包含许多“图片”的“帖子”模型

class Post < ActiveRecord::Base

  has_many :pictures, :dependent => :destroy

  accepts_nested_attributes_for :pictures, :allow_destroy => true, :reject_if => lambda { |t| t['pictures'].nil? }


  belongs_to :user 

  monetize :price

  validates :title, :brand, :model, :price, :year, presence: true

end

和我的图片模型:

class Picture < ActiveRecord::Base
  belongs_to :post

  has_attached_file :image,
    :path => ":rails_root/public/images/:id/:filename",
    :url  => "/images/:id/:filename",
    styles: { medium: "600x600>"}


  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
  validates :image, presence: true

end

我的创作是:

def create
  @post = current_user.post.build(post_params)

  respond_to do |format|
    if @post.save
      if params[:images]

        params[:images].each { |image|
        @post.pictures.create(image: image)
        }
      end
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
      format.json { render :show, status: :created, location: @post }
   else
      format.html { render :new }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end 

我的强项是:

  def post_params
  params.require(:post).permit(:title, :decription, :brand, :model, :price,  pictures_attributes: [ :image, :_destroy])
end

我的表格:

 = simple_form_for @post, html: { multipart: true } do |f|


.form-group
  = f.input :title, input_html: { class: 'form-control'}

.form-group
  = f.input :brand, input_html: { class: 'form-control'}

.form-group
  = f.input :model, input_html: { class: 'form-control'}

.form-group
  = f.input :description, input_html: { class: 'form-control'}

.form-group
  = f.input :year,  input_html: { class: 'form-control'} 

.form-group
  = f.input :price,  input_html: { class: 'form-control'}



.form-group
  = f.simple_fields_for :pictures do |pic|
    = render 'picture_fields', f: pic

.links
  = link_to_add_association 'add pictures', f, :pictures, class: "btn btn-default"



= f.button :submit, class: "btn btn-primary"

.form-inline.clearfix
.nested-fields
    = f.file_field :picture
    = link_to_remove_association "delete", f

当我加热提交按钮时,帖子被保存但无法迭代将帖子图片扔到节目中。 我的代码有错误吗?有没有办法知道图片是否保存?当我使用 ruby​​ 控制台时,@post = Post.last 和 @post.pictures 给了我这个:

图片加载 (2.0ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."post_id" = ? [[“post_id”,34]] => #

【问题讨论】:

    标签: ruby-on-rails nested-forms cocoon-gem


    【解决方案1】:

    这里有(至少)三个问题:

    首先,您不需要这样做(除非自然有其他方式可以使用相同的控制器)。 params[:images] 始终为零:

    if params[:images]
      params[:images].each { |image|
        @post.pictures.create(image: image)
      }
    end
    

    第二件事,您的模型似乎有一个属性 image 但是在您的表单中您有:

    = f.file_field :picture
    

    老实说,我希望这会引发异常,这可能是您的问题的原因。 (如果是这种情况 - 您应该始终发布您遇到的任何异常情况)。

    第三件事:这绝对不是您要解决的问题,但很可能在解决此问题后您将发布的下一个问题:您在强大的参数中缺少pictures_attributes 中的id - 这会导致您在每次提交表单时复制所有现有图像 + 将无法删除图片。

    编辑:

    其他几个潜在问题。您应该始终将茧关联包装在一些包装器中 - button_to_add_association 后面的 javascript 期望某些 DOM 结构,可能会被覆盖,但使用默认值更容易。

     #pictures
      .form-group
        = f.simple_fields_for :pictures do |pic|
          = render 'picture_fields', f: pic
    
      .links
        = link_to_add_association 'add pictures', f, :pictures, class: "btn btn-default"
    

    另一个潜在的问题是你的偏心。 link_to_remove_association 查找具有类 nested-fields 的最接近的父级并将其删除(除非它是现有记录,在这种情况下它会添加一些额外的内容并将其隐藏,不相关)。问题是,如果您添加 5 张图片,然后将它们全部删除 - 您将剩下 5 张 .form-inline.clearfix 的东西。我没有看到您的 CSS,但这可能会使您的视图不一致/损坏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2015-10-14
      相关资源
      最近更新 更多