|
| 1 | +<div id="product_form"> |
| 2 | + <%= form_with model: product, url: products_path, class: "space-y-4" do |f| %> |
| 3 | + <% if product.errors.any? %> |
| 4 | + <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded"> |
| 5 | + <ul> |
| 6 | + <% product.errors.full_messages.each do |message| %> |
| 7 | + <li><%= message %></li> |
| 8 | + <% end %> |
| 9 | + </ul> |
| 10 | + </div> |
| 11 | + <% end %> |
| 12 | + |
| 13 | + <div> |
| 14 | + <%= f.label :product_id, "Select Existing Product (or create new)", class: "block text-gray-700 text-sm font-bold mb-2" %> |
| 15 | + <%= select_tag :product_id, |
| 16 | + options_from_collection_for_select(products, :id, :name, ""), |
| 17 | + { |
| 18 | + include_blank: "Create New Product", |
| 19 | + class: "shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline", |
| 20 | + id: "product_select", |
| 21 | + onchange: "toggleProductNameField()" |
| 22 | + } %> |
| 23 | + </div> |
| 24 | + |
| 25 | + <div id="product_name_field"> |
| 26 | + <%= f.label :name, "Product Name", class: "block text-gray-700 text-sm font-bold mb-2" %> |
| 27 | + <%= f.text_field :name, |
| 28 | + class: "shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline", |
| 29 | + id: "product_name_input" %> |
| 30 | + </div> |
| 31 | + |
| 32 | + <div> |
| 33 | + <%= f.label :images, "Images", class: "block text-gray-700 text-sm font-bold mb-2" %> |
| 34 | + <%= f.file_field :images, |
| 35 | + multiple: true, |
| 36 | + accept: "image/*", |
| 37 | + class: "block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100" %> |
| 38 | + <p class="text-xs text-gray-500 mt-1">You can select multiple images</p> |
| 39 | + </div> |
| 40 | + |
| 41 | + <div> |
| 42 | + <%= f.submit "Upload Images", class: "bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline cursor-pointer" %> |
| 43 | + </div> |
| 44 | + <% end %> |
| 45 | +</div> |
| 46 | + |
| 47 | +<script> |
| 48 | +function toggleProductNameField() { |
| 49 | + const select = document.getElementById('product_select'); |
| 50 | + const nameField = document.getElementById('product_name_field'); |
| 51 | + const nameInput = document.getElementById('product_name_input'); |
| 52 | + |
| 53 | + if (select.value === '' || select.value === 'new') { |
| 54 | + // Show name field and make it required for new products |
| 55 | + nameField.style.display = 'block'; |
| 56 | + nameInput.required = true; |
| 57 | + } else { |
| 58 | + // Hide name field and make it optional when attaching to existing |
| 59 | + nameField.style.display = 'none'; |
| 60 | + nameInput.required = false; |
| 61 | + nameInput.value = ''; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Initialize on page load |
| 66 | +document.addEventListener('DOMContentLoaded', function() { |
| 67 | + toggleProductNameField(); |
| 68 | +}); |
| 69 | +</script> |
| 70 | + |
0 commit comments