diff --git a/app/controllers/reviews_controller.rb b/app/controllers/reviews_controller.rb index ea9e769..d47f42f 100644 --- a/app/controllers/reviews_controller.rb +++ b/app/controllers/reviews_controller.rb @@ -33,9 +33,26 @@ def create location_id: @location.id ) else - flash[:error_save_review] = "Couldn't save your review" - - render :new + respond_to do |format| + format.html do + flash[:error_save_review] = "Couldn't save your review" + + render :new + end + + format.turbo_stream do + flash.now[:error_save_review] = "Couldn't save your review" + + render turbo_stream: turbo_stream.replace( + "review-form", + partial: "reviews/shared/form", + locals: { + location: @location, + review: @review + } + ) + end + end end end @@ -62,9 +79,26 @@ def update location_id: @location.id ) else - flash[:error_save_review] = "Couldn't save your review" - - render :new + respond_to do |format| + format.html do + flash[:error_save_review] = "Couldn't save your review" + + render :edit + end + + format.turbo_stream do + flash.now[:error_save_review] = "Couldn't save your review" + + render turbo_stream: turbo_stream.replace( + "review-form", + partial: "reviews/shared/form", + locals: { + location: @location, + review: @review + } + ) + end + end end end diff --git a/app/views/reviews/shared/_form.html.erb b/app/views/reviews/shared/_form.html.erb index b04f622..1d45552 100644 --- a/app/views/reviews/shared/_form.html.erb +++ b/app/views/reviews/shared/_form.html.erb @@ -1,140 +1,142 @@ -<% if flash[:error_save_review].present? %> - -<% end %> +<%= turbo_frame_tag "review-form" do %> + <% if flash[:error_save_review].present? %> + + <% end %> -<% if flash[:error_generate_review].present? %> - -<% end %> + <% if flash[:error_generate_review].present? %> + + <% end %> -<%= form_for [location, review], data: { turbo_frame: "_top" } do |f| %> -
- <%= f.label :overall, class: "col-2 col-form-label" %> -
- <%= f.select( - :overall, - helper_review_rating_options, - { - include_blank: "", - selected: review.overall - }, - { - class: "form-select#{review.errors[:overall].any? ? ' is-invalid' : ''}", - aria: { - label: "Overall rating", + <%= form_for [location, review], data: { turbo_frame: "_top" } do |f| %> +
+ <%= f.label :overall, class: "col-2 col-form-label" %> +
+ <%= f.select( + :overall, + helper_review_rating_options, + { + include_blank: "", + selected: review.overall + }, + { + class: "form-select#{review.errors[:overall].any? ? ' is-invalid' : ''}", + aria: { + label: "Overall rating", + } } - } - ) %> + ) %> +
-
-
- <%= f.label :fun, class: "col-2 col-form-label" %> -
- <%= f.select( - :fun, - helper_review_rating_options, - { - include_blank: "", - selected: review.fun - }, - { - class: "form-select#{review.errors[:fun].any? ? ' is-invalid' : ''}", - aria: { - label: "Fun rating", +
+ <%= f.label :fun, class: "col-2 col-form-label" %> +
+ <%= f.select( + :fun, + helper_review_rating_options, + { + include_blank: "", + selected: review.fun + }, + { + class: "form-select#{review.errors[:fun].any? ? ' is-invalid' : ''}", + aria: { + label: "Fun rating", + } } - } - ) %> + ) %> +
-
-
- <%= f.label :cost, class: "col-2 col-form-label" %> -
- <%= f.select( - :cost, - helper_review_rating_options, - { - include_blank: "", - selected: review.cost - }, - { - class: "form-select#{review.errors[:cost].any? ? ' is-invalid' : ''}", - aria: { - label: "Cost rating", +
+ <%= f.label :cost, class: "col-2 col-form-label" %> +
+ <%= f.select( + :cost, + helper_review_rating_options, + { + include_blank: "", + selected: review.cost + }, + { + class: "form-select#{review.errors[:cost].any? ? ' is-invalid' : ''}", + aria: { + label: "Cost rating", + } } - } - ) %> + ) %> +
-
-
- <%= f.label :internet, class: "col-2 col-form-label" %> -
- <%= f.select( - :internet, - helper_review_rating_options, - { - include_blank: "", - selected: review.internet - }, - { - class: "form-select#{review.errors[:internet].any? ? ' is-invalid' : ''}", - aria: { - label: "Internet rating", +
+ <%= f.label :internet, class: "col-2 col-form-label" %> +
+ <%= f.select( + :internet, + helper_review_rating_options, + { + include_blank: "", + selected: review.internet + }, + { + class: "form-select#{review.errors[:internet].any? ? ' is-invalid' : ''}", + aria: { + label: "Internet rating", + } } - } - ) %> + ) %> +
-
-
- <%= f.label :safety, class: "col-2 col-form-label" %> -
- <%= f.select( - :safety, - helper_review_rating_options, - { - include_blank: "", - selected: review.safety - }, - { - class: "form-select#{review.errors[:safety].any? ? ' is-invalid' : ''}", - aria: { - label: "Safety rating", +
+ <%= f.label :safety, class: "col-2 col-form-label" %> +
+ <%= f.select( + :safety, + helper_review_rating_options, + { + include_blank: "", + selected: review.safety + }, + { + class: "form-select#{review.errors[:safety].any? ? ' is-invalid' : ''}", + aria: { + label: "Safety rating", + } } - } - ) %> + ) %> +
-
- <%= f.label :body, "Summary", class: "form-label"%> - <%= f.text_area :body, - class: "form-control mb-3", - rows: 6 - %> - <%= f.button( - "Finish", - type: :submit, - class: "btn btn-primary mb-2", - ) %> -<% end %> + <%= f.label :body, "Summary", class: "form-label"%> + <%= f.text_area :body, + class: "form-control mb-3", + rows: 6 + %> + <%= f.button( + "Finish", + type: :submit, + class: "btn btn-primary mb-2", + ) %> + <% end %> -<% if current_user.admin? %> - <%= button_to( - "Auto-generate review", - generate_review_location_reviews_path( - review, location_id: location.id - ), - method: :post, - class: "btn btn-secondary", - data: { - turbo: false, - } - ) %> + <% if current_user.admin? %> + <%= button_to( + "Auto-generate review", + generate_review_location_reviews_path( + review, location_id: location.id + ), + method: :post, + class: "btn btn-secondary", + data: { + turbo: false, + } + ) %> + <% end %> <% end %>