2016-03-28 16 views
0

Kann nicht nachvollziehen, wie die assoziierte Modellgröße beim Aktualisieren des Hauptmodells überprüft wird. In meiner App darf das Produkt nicht mehr als 6 angehängte Bilder enthalten. Jedes Bild speichere ich in der Klasse ProductAttachmentSo überprüfen Sie die Anzahl der zugeordneten Objekte während der Aktualisierung des Modells

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 

und in products_controller Ich habe Methode ein erstellen bekam

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 

die Fein- und Schöpfung arbeitet abgelehnt, wenn ich versuche, mehr als 6 Bilder zu befestigen, aber Update-Methode Anzahl von atachments irgendwie nicht bestimmen kann, und lassen Sie alle Nummern der Bilder sollte ich somemething in verändern

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 

Ich denke, hinzufügen products_params Methode, aber ich kann nicht herausgefunden, was genau

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  

dies eine Form ist

<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> 

Antwort

0

Sie manuell diese Validierung durch Anzeige auslösen können ding einen explizit @ product.save Aufruf innerhalb des glücklichen Pfades von #update.

Verwandte Themen