0

I have models products and gallery with has_many belongs_to relation, I'm implementing nested_attributes feature, Problem here is: When I click on add gallery two inner forms get created instead of one, as shown in below image:enter image description here

CODE

    form do |f|
    f.inputs "Product" do
        f.input :title, :required => true
        f.input :description
        f.input :price
        f.input :display_image
        f .input :product_detail, :hint => "Upload PDF file"
        f.input :category
    end

    f.inputs 'Product Gallery' do
        f.has_many :galleries, allow_destroy: true, new_record: 'Add Gallery' do |c|
            c.input :image, :hint => c.template.image_tag(c.object.image.url(:thumb)) 
        end
    end
    f.inputs 'Product Specification' do
        f.has_many :specifications, allow_destroy: true, new_record: true do |c|
            c.input :specification_label
            c.input :specification_details
        end
    end
    f.actions
end

I need help on this. I'm not able to solve this!!, any help would be appreciable.

9
  • I cannot see your code, but I encounter the same before, you should put the add fields link/button that generates dynamic fields, outside of the f.fields_for :galleries Commented Aug 9, 2016 at 6:51
  • Hi @aldrien.h thanks for the reply, I'm not using f.fields_for anywhere in my code, I edited my question and added the code! Commented Aug 9, 2016 at 7:00
  • i think because you have two loops, that's why it generates twice. Commented Aug 9, 2016 at 7:04
  • did you added ` accepts_nested_attributes_for` in model? Commented Aug 9, 2016 at 7:04
  • what parameter are you permit in this form? Commented Aug 9, 2016 at 7:05

1 Answer 1

1

Try this.

   f.inputs 'Product Gallery' do
      f.has_many :galleries do |c|
        c.input :image, :hint => c.template.image_tag(c.object.image.url(:thumb)) 
        c.input :_destroy, :as => :boolean
      end
    end

Hope this will work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering @hgsongra , I tried this but no luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.