fix saving of subproject_address

This commit is contained in:
Stefan Tollkühn
2025-07-23 17:11:33 +02:00
parent 6d60abee4d
commit 338b52a560
3 changed files with 20 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ class ProjectsController < ApplicationController
subproject.build_client subproject.build_client
subproject.build_owner subproject.build_owner
subproject.build_builder subproject.build_builder
subproject.build_subproject_address
end end
end end
@@ -66,6 +67,7 @@ class ProjectsController < ApplicationController
private private
CLIENT_FIELDS = [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone] CLIENT_FIELDS = [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone]
SUBPROJECT_ADDRESS_FIELDS = [:streetname, :zipcode, :city, :country]
# Use callbacks to share common setup or constraints between actions. # Use callbacks to share common setup or constraints between actions.
def set_project def set_project
@@ -80,7 +82,8 @@ class ProjectsController < ApplicationController
:id, :subproject_name, :client_id, :owner_id, :builder_id, :_destroy, :id, :subproject_name, :client_id, :owner_id, :builder_id, :_destroy,
client_attributes: CLIENT_FIELDS, client_attributes: CLIENT_FIELDS,
owner_attributes: CLIENT_FIELDS, owner_attributes: CLIENT_FIELDS,
builder_attributes: CLIENT_FIELDS builder_attributes: CLIENT_FIELDS,
subproject_address_attributes: SUBPROJECT_ADDRESS_FIELDS
] ]
) )
end end

View File

@@ -5,12 +5,11 @@ class Subproject < ApplicationRecord
belongs_to :client, class_name: 'Client', optional: true belongs_to :client, class_name: 'Client', optional: true
belongs_to :owner, class_name: 'Client', optional: true belongs_to :owner, class_name: 'Client', optional: true
belongs_to :builder, class_name: 'Client', optional: true belongs_to :builder, class_name: 'Client', optional: true
belongs_to :subproject_addresses, optional: true
accepts_nested_attributes_for :client, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) } accepts_nested_attributes_for :client, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) }
accepts_nested_attributes_for :owner, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) } accepts_nested_attributes_for :owner, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) }
accepts_nested_attributes_for :builder, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) } accepts_nested_attributes_for :builder, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) }
accepts_nested_attributes_for :subproject_addresses, allow_destroy: true, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) } accepts_nested_attributes_for :subproject_address, allow_destroy: true, reject_if: ->(attrs) { attrs['id'].present? || attrs.values.all?(&:blank?) }
validates :subproject_name, presence: true validates :subproject_name, presence: true

View File

@@ -41,7 +41,22 @@
</div> </div>
<% end %> <% end %>
</div> </div>
<div class="subproject-address-fields">
<fieldset>
<legend>Address</legend>
<%= sp_form.fields_for :subproject_address do |spaddress_form| %>
<% [:streetname, :zipcode, :city, :country].each do |attrib| %>
<%= spaddress_form.label attrib %>
<%= spaddress_form.text_field attrib %><br>
<% end %> <% end %>
<% end %>
</fieldset>
</div>
<% end %>
</div>
<div> <div>
<%= form.submit %> <%= form.submit %>