1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| # product_types/_form.html.erb
<%= form_for(@product_type) do |f| %>
...
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :fields do |builder| %>
<%= render "field_fields", f: builder %>
<% end %>
<%= link_to_add_fields "Add Field", f, :fields %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
# add product_types/_product_field_fields.html.erb
<fieldset>
<%= f.select :field_type, %w[text_field check_box shirt_sizes] %>
<%= f.text_field :name, placeholder: "field_name" %>
<%= f.check_box :required %> <%= f.label :required %>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
# product_types/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @product_type.name %>
</p>
<p>
<strong>Fields:</strong>
<%= @product_type.fields.each do |f| %>
<br>
<%= f.name %>
<br>
<%= f.field_type %>
<% end -%>
</p>
...
# products/_form.html.erb
...
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<%= f.fields_for :properties, OpenStruct.new(@product.properties) do |builder| %>
<% @product.product_type.fields.each do |field| %>
<%= render "products/fields/#{field.field_type}", field: field, f: builder %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
# products/index.html.erb
...
<%= form_tag new_product_path, method: :get do %>
<%= select_tag :product_type_id, options_from_collection_for_select(ProductType.all, :id, :name) %>
<%= submit_tag "New Product", name: nil %>
<% end %>
|