diff --git a/app/controllers/start_form_controller.rb b/app/controllers/start_form_controller.rb index e4e493bb..938ca2ab 100644 --- a/app/controllers/start_form_controller.rb +++ b/app/controllers/start_form_controller.rb @@ -14,22 +14,20 @@ class StartFormController < ApplicationController def update @submitter = Submitter.where(submission: @template.submissions.where(deleted_at: nil)) + .order(id: :desc) .then { |rel| params[:resubmit].present? ? rel.where(completed_at: nil) : rel } .find_or_initialize_by(**submitter_params.compact_blank) if @submitter.completed_at? redirect_to start_form_completed_path(@template.slug, email: submitter_params[:email]) else - @submitter.assign_attributes( - uuid: @template.submitters.first['uuid'], - ip: request.remote_ip, - ua: request.user_agent, - preferences: { 'send_email' => true } - ) + if @template.submitters.to_a.size > 1 && @submitter.new_record? + @error_message = 'Not found' - @submitter.submission ||= Submission.new(template: @template, - template_submitters: @template.submitters, - source: :link) + return render :show + end + + assign_submission_attributes(@submitter, @template) if @submitter.new_record? if @submitter.save redirect_to submit_form_path(@submitter.slug) @@ -47,6 +45,21 @@ class StartFormController < ApplicationController private + def assign_submission_attributes(submitter, template) + submitter.assign_attributes( + uuid: template.submitters.first['uuid'], + ip: request.remote_ip, + ua: request.user_agent, + preferences: { 'send_email' => true } + ) + + submitter.submission ||= Submission.new(template:, + template_submitters: template.submitters, + source: :link) + + submitter + end + def submitter_params params.require(:submitter).permit(:email, :phone, :name).tap do |attrs| attrs[:email] = Submissions.normalize_email(attrs[:email]) diff --git a/app/views/start_form/completed.html.erb b/app/views/start_form/completed.html.erb index 90537c20..7259521c 100644 --- a/app/views/start_form/completed.html.erb +++ b/app/views/start_form/completed.html.erb @@ -24,9 +24,11 @@ <%= button_to button_title(title: 'Send copy to Email', disabled_with: 'Sending', icon: svg_icon('mail_forward', class: 'w-6 h-6')), send_submission_email_index_path, params: { submitter_slug: @submitter.slug }, form: { onsubmit: 'event.submitter.disabled = true' }, class: 'base-button w-full' %> <% end %> -