From d14700c51d692335f001a93c2f6b13b135783206 Mon Sep 17 00:00:00 2001 From: IN COMMON Collective Date: Thu, 8 Apr 2021 16:34:15 +0200 Subject: [FIX] Use form model to create/edit resources (fixes #4, fixes #5, refs #3) Since we must associate other models (e.g., classifications) to a Resource, we use a composite model to save all changes inside a database transaction. This approach makes it simpler to handle resources and their associations. Work remains to fix the geolocation and reverse geolocation to ensure these are in sync. --- app/views/resources/_edit.html.erb | 4 ++-- app/views/resources/_form.html.erb | 37 +++++++++++++++++++++++++------------ app/views/resources/new.html.erb | 4 +++- 3 files changed, 30 insertions(+), 15 deletions(-) (limited to 'app/views/resources') diff --git a/app/views/resources/_edit.html.erb b/app/views/resources/_edit.html.erb index 55c59f6..f22aee3 100644 --- a/app/views/resources/_edit.html.erb +++ b/app/views/resources/_edit.html.erb @@ -1,6 +1,6 @@ -

Edit <%= @resource.presence.feature['properties']['name'] || 'new resource' %>

+

Edit <%= @resource.presence.name || 'new resource' %>

Context: <%= current_agent %>

-<%= form_with model: [current_agent,@resource], url: controller.action_name == 'new' ? agent_resources_path(agent: current_agent) : resource_path(@resource) do |f| %> +<%= form_with model: @resource, url: @resource.presence.new_record? ? agent_resources_path(current_agent) : resource_path(@resource.resource) do |f| %> <%= render partial: 'form', locals: { resource: resource, f: f } %> <% end %> diff --git a/app/views/resources/_form.html.erb b/app/views/resources/_form.html.erb index 89745bd..648834d 100644 --- a/app/views/resources/_form.html.erb +++ b/app/views/resources/_form.html.erb @@ -1,12 +1,24 @@ -
+<% if resource.errors.any? %> +
+

<%= pluralize(resource.errors.count, "error") %> prohibited this resource from being saved:

+ +
    + <% resource.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+<% end %> + +
<%= tag.legend "Propriétés de la ressource" %> -
+
<%= f.label :name %>
-
<%= f.text_field :name, maxlength: 64, placeholder: 'La ferme des animaux', value: resource.name %>
+
<%= f.text_field :name, maxlength: 64, placeholder: 'La ferme des animaux' %>
<%= f.label :summary %>
-
<%= f.text_field :summary, maxlength: 136, placeholder: 'Une ferme locale', value: resource.name %>
+
<%= f.text_field :summary, maxlength: 136, placeholder: 'Une ferme locale' %>
<%= f.label :description %>
<%= f.text_area :description, cols: 72, rows: 10, placeholder: '## Un choix pertinent @@ -29,33 +41,34 @@ La description _peut_ comporter du [Markdown].
<%= tag.legend "Classification" %> <%# TODO Add a taxonomy selector %> - <%= section_select(current_agent.taxonomies&.first || Taxonomy.first) %> + <%= section_select(current_agent.taxonomies&.first || Taxonomy.first, resource.section_ids, name: 'resource_form[section_ids]') %>
<%= tag.legend "Coordonées géographiques" %> <%# TODO Add a graphical geo selector %> -
+
<%= f.label :address %>
-
<%= f.text_field :address, value: resource.address, placeholder: 'Rue aux herbes, 123' %>
+
<%= f.text_field :address, placeholder: 'Rue aux herbes, 123' %>
<%= f.label :postal_code %>
-
<%= f.text_field :postal_code, value: resource.postal_code, placeholder: '1000' %>
+
<%= f.text_field :postal_code, placeholder: '1000' %>
<%= f.label :city %>
-
<%= f.text_field :city, value: resource.city, placeholder: 'Bruxelles' %>
+
<%= f.text_field :city, placeholder: 'Bruxelles' %>
-
+
<%= f.label :longitude %>
-
<%= f.text_field :longitude, value: resource.longitude, placeholder: '0.56789012' %>
+
<%= f.text_field :longitude, placeholder: '0.56789012', pattern: '[\d]+.[\d]+' %>
<%= f.label :latitude %>
-
<%= f.text_field :latitude, value: resource.latitude, placeholder: '50.12345678' %>
+
<%= f.text_field :latitude, placeholder: '50.12345678', pattern: '[\d]+.[\d]+' %>
<%= f.hidden_field :agent_id, value: current_agent.id %> +<%= f.hidden_field :source, value: current_agent.name %>

<%= f.submit 'Save' %>

diff --git a/app/views/resources/new.html.erb b/app/views/resources/new.html.erb index 80d8206..e8347b4 100644 --- a/app/views/resources/new.html.erb +++ b/app/views/resources/new.html.erb @@ -1,2 +1,4 @@ -<%= render partial: 'edit', locals: { resource: @resource } %> +<%= render 'edit', resource: @resource %> + +<#%= render partial: 'edit', locals: { resource: @resource } %> -- cgit v1.2.3