如何在更新模型时验证关联的对象编号

时间:2016-03-28 08:11:50

标签: ruby-on-rails validation strong-parameters

无法了解在更新主模型时如何验证关联模型的大小。在我的应用产品中不能有超过6个附加图像。我在ProductAttachment类中存储的每个图像

product.rb

class Product < ActiveRecord::Base
belongs_to :user
has_many :product_attachments, dependent: :destroy
accepts_nested_attributes_for :product_attachments
validate :validate_attachments_count


  def validate_attachments_count
    if self.product_attachments.size > 6
      errors.add(:product_attachments, 'Pic number should not be more than 6')
    end
  end
end

product_attachment.rb

class ProductAttachment < ActiveRecord::Base
mount_uploader :picture, PictureUploader
belongs_to :product
after_destroy :delete_picture

private
  def delete_picture
    self.remove_picture!
  end
end  

并且在products_controller中我有一个创建方法

def create
  @product = current_user.products.new(products_params)
  # if pics available add attachments
  if params[:product_attachments] != nil
    params[:product_attachments]['picture'].each do |p|
      @product_attachment = @product.product_attachments.build(:picture => p)
    end
  end
  if @product.save
      flash[:success] = 'Product created.'
      redirect_to [current_user, @product]
  else
      flash[:danger] = 'Product not created'
      render 'new'
  end
end

如果我尝试附加超过6张图片,效果很好并且创作被拒绝,但更新方法无法以某种方式确定附件数量并允许添加任意数量的图片

def update
  if @product.update_attributes(products_params)
    if params[:product_attachments] != nil
      params[:product_attachments]['picture'].each do |p|
        @product_attachment = @product.product_attachments.create(:picture => p)
      end
    end
    flash[:success] = 'Info updated.'
    redirect_to [current_user, @product]
  else
    flash[:danger] = 'Can't update'
    render :edit
  end
end 

我想我应该在products_params方法中改变一些东西,但我无法弄明白究竟是什么

def products_params
  params.require(:product).permit(:name, :width, :height, :depth, :color, :price, :category, :description,
  product_attachments_attributes: [:id, :product_id, :picture, :remove_picture])
end     

这是表格

<div class="form-group">
    <% if @product.new_record? %>
      <%= f.fields_for :product_attachments do |p| %>
        <div class="control-label col-md-4">
          <%= p.label :picture, 'Pictures' %>
        </div>
        <div class="col-md-4">
          <%= p.file_field :picture, multiple: true, name: 'product_attachments[picture][]' %>
        </div>
      <% end %>
    <% else %>
        <div class="control-label col-md-4"><strong>Pictures</strong></div>
        <div class="col-md-4">
          <% @product.product_attachments.each do |p| %>
            <%= image_tag p.picture_url, class: 'pic' %>
            <%= link_to 'Delete', product_attachment_path(p),
                        method: :delete , data: { confirm: 'Sure?'} %>
          <% end %>
          <%= f.fields_for :product_attachments, @product.product_attachments.new do |p| %>
            <div class="col-md-4">
              <%= p.file_field :picture, multiple: true, name: 'product_attachments[picture][]' %>
            </div>
          <% end %>
        </div>
    <% end %>
  </div>

1 个答案:

答案 0 :(得分:0)

您可以通过在#update的快乐路径中添加显式@ product.save调用来手动触发该验证。