Create action fails, no errors displayed after template render

37 Views Asked by At

This functionality was working when I implemented it a month ago and I have no clue why it stopped functioning. I didn't make any changes to anything related, but I guess we have all been there and taught differently ;)

I am working with a review form located inside my bookings show view. It was, back then, perfectly displaying the error messages when the validations failed. Now, when the @review.save fails, it renders the template of bookings/show, but doesn't show any errors for the user. Not even my console shows any.

partials/new-review.html.erb

<section class="review-form">
    <% if @host == current_user %>
        <h2>Let <%= @booking.user.first_name %> & us know about your experience!</h2>
    <% else %>
        <h2>Let <%= @host.first_name %> & us know about your experience!</h2>
    <% end %>

    <%= simple_form_for [@couch, @booking, @review] do |f| %>
        <div class="review-form__action">
            <%= f.input :rating,
                                  collection: (1..5).to_a,
                                  label: false,
                                  input_html: { data: {controller: 'star-rating' } } %>
            <%= f.error :rating, class: 'form-error form-error-rating' %>
            <%= f.label 'Leave some words' %>
            <%= f.input :content, 
                                  label: false,
                                  input_html: { rows: 6 } %>
            <%= f.error :content, class: 'form-error' %>
            <%= f.submit 'Submit Review', class: 'review-form__button' %>
        </div>
    <% end %>
</section>

review.rb

validates :content, presence: { message: 'Please add a few lines about your experience' }
validates :rating, presence: { message: 'Please leave a rating' }

reviews_controller.rb

if review.save
  things happen
else
  render 'bookings/show', locals: { booking: }
end

--> this if statement is happening inside another method, thats why the variables are no instance variables. don't worry about this, i checked everything and the variables are working as expected.

I feel like the rendering render 'bookings/show', locals: { booking: } is the issue because it is literally rendering a new show page instead of staying on the show page and rendering the errors. What am I missing?

1

There are 1 best solutions below

0
Lisbeth Purrucker On

I simply added unprocessable_entity to it and it showed the error messages again.

render 'bookings/show', locals: { booking: }, status: :unprocessable_entity