From 14f4c063de3052242af73d6ca261b347e3934b6e Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Fri, 3 Apr 2020 18:34:39 +0200 Subject: [PATCH] Fixed creating/updating with MeasurementRoutine Updated Measurement links with :view --- app/controllers/measurements_controller.rb | 12 ++++++++---- app/helpers/measurements_helper.rb | 6 +++--- app/views/measurement_routines/_show.html.erb | 3 --- app/views/measurement_routines/_show_form.html.erb | 13 +++++++------ app/views/measurement_routines/show.js.erb | 2 +- app/views/measurements/_index.html.erb | 2 +- app/views/measurements/_readouts.html.erb | 6 ++++-- 7 files changed, 24 insertions(+), 20 deletions(-) delete mode 100644 app/views/measurement_routines/_show.html.erb diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb index 4f2b779..5bc41b6 100644 --- a/app/controllers/measurements_controller.rb +++ b/app/controllers/measurements_controller.rb @@ -27,10 +27,8 @@ class MeasurementsController < ApplicationController # existing nested object (MeasurementRoutine) if it's not associated with outer object # https://stackoverflow.com/questions/6346134/ # That's why routine needs to be found and associated before measurement initialization - @measurement = @project.measurements.new do |m| - routine_id = params[:measurement][:routine_attributes][:id] - m.routine = @project.measurement_routines.find_by(id: routine_id) if routine_id - end + @measurement = @project.measurements.new + update_routine_from_params @measurement.attributes = measurement_params @measurement.routine.project = @project @routine = @measurement.routine @@ -51,6 +49,7 @@ class MeasurementsController < ApplicationController end def update + update_routine_from_params if @measurement.update(measurement_params) flash[:notice] = 'Updated measurement' prepare_items @@ -117,6 +116,11 @@ class MeasurementsController < ApplicationController ) end + def update_routine_from_params + routine_id = params[:measurement][:routine_attributes][:id] + @measurement.routine = @project.measurement_routines.find_by(id: routine_id) if routine_id + end + def prepare_items params[:view] == 'index' ? prepare_measurements : prepare_readouts end diff --git a/app/helpers/measurements_helper.rb b/app/helpers/measurements_helper.rb index 2be63b2..8e1f1b5 100644 --- a/app/helpers/measurements_helper.rb +++ b/app/helpers/measurements_helper.rb @@ -31,10 +31,10 @@ module MeasurementsHelper end end - def action_links(m) - link_to(l(:button_retake), retake_measurement_path(m), + def action_links(m, view) + link_to(l(:button_retake), retake_measurement_path(m, view: view), {remote: true, class: "icon icon-reload"}) + - link_to(l(:button_edit), edit_measurement_path(m), + link_to(l(:button_edit), edit_measurement_path(m, view: view), {remote: true, class: "icon icon-edit"}) + delete_link(measurement_path(m), {remote: true, data: {}}) end diff --git a/app/views/measurement_routines/_show.html.erb b/app/views/measurement_routines/_show.html.erb deleted file mode 100644 index 57d9bc1..0000000 --- a/app/views/measurement_routines/_show.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -<% unless @routine.description.empty? %> -

<%= @routine.description %>

-<% end %> diff --git a/app/views/measurement_routines/_show_form.html.erb b/app/views/measurement_routines/_show_form.html.erb index 3a91dd0..8d8ecaf 100644 --- a/app/views/measurement_routines/_show_form.html.erb +++ b/app/views/measurement_routines/_show_form.html.erb @@ -1,18 +1,19 @@
-<%= f.fields_for :routine do |ff| %> +<%= fields_for 'measurement[routine_attributes]', @routine do |ff| %>

<%= ff.select :id, options_from_collection_for_select(@project.measurement_routines, :id, :name, @routine.id), {label: :field_measurement_routine, required: true}, - onchange: "var mr_id = $('#measurement_routine_attributes_id option:selected').val(); + autocomplete: 'off', + onchange: "var mr_id = $('#measurement_routine_attributes_id').val(); $.ajax({ url: '#{measurement_routine_path(id: :mr_id)}'.replace('mr_id', mr_id), dataType: 'script' }); return false;" %> <%= link_to l(:button_edit), '#', - onclick: "var mr_id = $('#measurement_routine_attributes_id option:selected').val(); + onclick: "var mr_id = $('#measurement_routine_attributes_id').val(); $.ajax({ url: '#{edit_measurement_routine_path(id: :mr_id)}'.replace('mr_id', mr_id), dataType: 'script' @@ -21,7 +22,7 @@ class: 'icon icon-edit' %>

<% end %> -
- <%= render partial: 'measurement_routines/show' %> -
+<% unless @routine.description.empty? %> +

<%= @routine.description %>

+<% end %>
diff --git a/app/views/measurement_routines/show.js.erb b/app/views/measurement_routines/show.js.erb index 0bb0ad2..66f83e6 100644 --- a/app/views/measurement_routines/show.js.erb +++ b/app/views/measurement_routines/show.js.erb @@ -1 +1 @@ -$('#measurement-routine').html('<%= j render partial: 'measurement_routines/show' %>'); +$('#measurement-routine-form').html('<%= j render partial: 'measurement_routines/show_form' %>'); diff --git a/app/views/measurements/_index.html.erb b/app/views/measurements/_index.html.erb index a404dc6..ee2ff5d 100644 --- a/app/views/measurements/_index.html.erb +++ b/app/views/measurements/_index.html.erb @@ -27,7 +27,7 @@ <%= m.source.name if m.source.present? %> - <%= action_links(m) %> + <%= action_links(m, :index) %> <% end %> diff --git a/app/views/measurements/_readouts.html.erb b/app/views/measurements/_readouts.html.erb index 4e9b38e..07db0c6 100644 --- a/app/views/measurements/_readouts.html.erb +++ b/app/views/measurements/_readouts.html.erb @@ -38,7 +38,7 @@ <% @requested_r[index].each do |*, value| %> <%= format_value(value) %> <% end %> - <%= action_links(m) %> + <%= action_links(m, :readouts) %> @@ -57,7 +57,9 @@

<%= format_value(value) %>

<% end %> - <%= action_links(m) %> + + <%= action_links(m, :readouts) %> + <% next unless @quantities.length > 0 %>