我有一个表单,用于选择Received
关系中的关联。它可以工作,但是当验证错误发生时,由于某种原因,它会将选择框呈现两次。
relationships.rb(Spree ::产品装饰)
Returns
look_book_image.rb
has_many through:
look_book_image_product.rb
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
_form.html.erb
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_fields.html.erb
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
look_book_images_controller.rb
<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>
非常感谢任何帮助。干杯:)
答案 0 :(得分:0)
找到解决方案,在控制器中:
@look_book_image = @look_book.look_book_images.build(look_book_image_params)
@look_book_image.attributes = look_book_image_params