2016-07-22 14 views
0

Ich habe ein Formular, das eine Zuordnung für eine has_many through: Beziehung auswählt. Es funktioniert, aber wenn Validierungsfehler auftreten, wird die Auswahlbox aus irgendeinem Grund zweimal gerendert.Rails simple_form Auswahlfeld erscheint zweimal bei Validierungsfehler

relationships.rb (Spree :: Produktdekoration)

module Products 
    module Relationships 
    extend ActiveSupport::Concern 

    included do 
     has_many :look_book_image_products, class_name: "Spree::LookBookImageProduct" 
     has_many :look_book_images, through: :look_book_image_products 

     def spree_product_id 
     self.id 
     end 

     def self.for_select(relation = nil) 
     relation ||= all 
     relation.pluck(:name, :id) 
     end 
    end 
    end 
end 

look_book_image.rb

module Spree 
    class LookBookImage < Asset 

    default_scope { order("#{self.table_name}.position") } 

    belongs_to :look_book 
    has_many :look_book_image_products, class_name: "Spree::LookBookImageProduct" 
    has_many :spree_products, through: :look_book_image_products 

    accepts_nested_attributes_for :look_book_image_products 

    before_destroy { clear_associated_objects } 
    before_save :find_dimensions, if: :attachment_updated_at_changed? 

    has_attached_file :attachment, styles: { attachment: 'x200>' }, 
     default_style: :attachment, bucket: ENV['S3_BUCKET_NAME'] 

    validate :no_attachment_errors 
    validates_presence_of :alt_text  
    validates :attachment, attachment_presence: true, 
    attachment_content_type: { 
     content_type: ['image/jpg', 'image/jpeg'] 
    } 

    def self.table_name 
     'look_book_images' 
    end 

    def remove_associated_product(product) 
     products.delete(product) 
    end 

    def find_dimensions 
     temporary = attachment.queued_for_write[:original] 
     filename = temporary.path unless temporary.nil? 
     filename = attachment.path if filename.blank? 
     geometry = Paperclip::Geometry.from_file(filename) 
     self.attachment_width = geometry.width 
     self.attachment_height = geometry.height 
    end 

    def mini_url 
     attachment.url(:mini, false) 
    end 

    def no_attachment_errors 
     unless attachment.errors.empty? 
     errors.clear 
     errors.add :attachment, "Paperclip returned errors for file '#{attachment_file_name}' - check ImageMagick installation or image source file." 
     false 
     end 
    end 

    def clear_associated_objects 
     clear_spree_products && clear_look_books 
    end 

    private 

    def clear_spree_products 
     if spree_products 
     spree_products.clear 
     end 
    end 

    def clear_look_books 
     if look_books 
     look_books.clear 
     end 
    end 
    end 
end 

look_book_image_product.rb

module Spree 
    class LookBookImageProduct < ActiveRecord::Base 
    belongs_to :look_book_image 
    belongs_to :spree_product 

    def self.table_name 
     'look_book_image_products' 
    end 
    end 
end 

_form.html.erb

<div data-hook="admin_image_form_fields" class="row"> 
    <div class="col-md-6"> 
    <%= f.field_container :attachment, class: ['form-group'] do %> 
     <%= f.label :attachment, Spree.t(:filename) %> 
     <%= f.file_field :attachment %> 
    <% end %> 

    <%= f.field_container :product, class: ['form-group'] do %> 
     <% f.fields_for :look_book_image_products do |ff| %> 
     <%= render partial: "look_book_image_product_fields", locals: { f: ff } %> 
      <div class="cocoon-links"> 
        <%= link_to_add_association 'Add another product', f, :look_book_image_products, class: 'btn btn-success', data: cocoon_data_options %> 
       </div> 
     <% end %> 
    <% end %> 

    <%= f.field_container :alt_text, class: ['form-group'] do %> 
     <%= f.label :alt_text, Spree.t(:alt_text) %> 
     <%= f.text_field :alt_text, class: 'form-control' %> 
    <% end %> 
    </div> 
</div> 

_look_book_image_product_fields.html.erb

<div class="nested-fields form-inline"> 
    <div class="form-group"> 
    <%= f.label :product, class: 'sr-only' %> 
    <%= f.association :spree_product, collection: Spree::Product.for_select, as: :select, item_wrapper_class: 'form-control input-sm' %> 
    </div> 
    <%= link_to_remove_association "Remove", f, class: 'btn btn-danger btn-sm', data: cocoon_data_options %> 
</div> 

look_book_images_controller.rb

module Spree 
    module Admin 
    class LookBookImagesController < ResourceController 
     before_action :set_look_book 
     before_action :set_look_book_image, only: %i(edit update destroy) 

     respond_to :html 

     def index 
     @look_book_images = @look_book.look_book_images.all 
     end 

     def new 
     @look_book_image = @look_book.look_book_images.build 
     @look_book_image.look_book_image_products.build 
     end 

     def edit; end 

     def create 
     @look_book_image = @look_book.look_book_images.build(look_book_image_params) 
     @look_book_image.attributes = look_book_image_params 

     if @look_book_image.save 
      flash[:success] = "Image has been created." 
      redirect_to admin_look_book_look_book_images_path 
     else 
      invoke_callbacks(:create, :fails) 
      respond_with(@look_book_image) do |format| 
      format.html { render action: :new } 
      format.js { render layout: false } 
      end 
     end 
     end 

     def destroy 
     @look_book_image.destroy 
     end 

     def update_positions 
     ActiveRecord::Base.transaction do 
      params[:positions].each do |id, index| 
      model_class.find(id).set_list_position(index) 
      end 
     end 

     respond_to do |format| 
      format.js { render text: 'Ok' } 
     end 
     end 

     private 

     def set_look_book_image 
     @look_book_image = @look_book.look_book_images.find(params[:id]) 
     rescue ActiveRecord::RecordNotFound 
     flash[:alert] = "The image you were looking for could not be found." 
     end 

     def set_look_book 
     @look_book = LookBook.find(params[:look_book_id]) 
     end 

     def look_book_image_params 
     params.require(:look_book_image).permit(:alt_text, :attachment, :look_book_id, look_book_image_products_attributes: [:id, :look_book_image_id, :spree_product_id, :_destroy]) 
     end 
    end 
    end 
end 

Jede Hilfe sehr geschätzt würde. Cheers :)

Antwort

0

die Lösung gefunden, war in der Steuerung:

@look_book_image = @look_book.look_book_images.build(look_book_image_params) 
@look_book_image.attributes = look_book_image_params 
Verwandte Themen