1

I am using rails 7.3 I am trying to see the errors in the browser when a prodcut can't be created. I am working on an aplicattion that has a restaurant, category and a product models.

The associations are as follows:

Product belongs_to :restaurant

Product belongs_to :category

Restaurant has_many :products

Restaurant has_many :categories

Category has_many :products

Category belongs_to :restaurant

I have a form that creates/edits a product and it allows me to create a Category on the fly if needed. This is the form.

<div class="card-body rounded">
  <%= form_with(model: [@restaurant, @product]) do |form| %>
    <p class="red"><i><b>Note: </b>Labels with (<b> ** </b>) are madatory </i></p>
    <% if @product.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
        <ul>
          <% @product.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
        </ul>
      </div>
    <% end %>
    <%= form.hidden_field :restaurant_id, value: @restaurant.id %>
    <!--- FROM HERE THE FORM --->
    <div class="row mb-4">
      <div class="col-sm-5  align-right label">
        <%= form.label :name, '** Product Name' %>
      </div>
      <div class="col-sm-7 form-input">
        <%= form.text_field :name, id: "product_name", placeholder: "Enter name", oninput: "checkInput()" %>
      </div>
    </div>
    <div class="text-center mb-4 red">
      <p><i>You can either <b> Select</b> or Create a <b> New Category </b>. You can not use both. </i> </p>
    </div>
    <div class="row mt-3 mb-3">
      <div class="col-sm-5 align-right label">
        <%= form.label :Category, "** Select Category" %>
      </div>
      <div class="col-sm-7 form-input">
        <%= form.collection_select :category_id, @restaurant.categories, :id, :name, { prompt: 'Select existing category' }, { class: "collection-select", id: "product_category_id" , oninput: "checkInput()"}  %>
      </div>
    </div>
    <div class="text-center mb-3">
      <b> -- OR -- </b>
    </div>
    <div class="row mb-4">
      <div class="col-sm-5 align-right label">
        <%= form.label :new_category_name, "** Create a New Category:" , class: 'label' %>
      </div>
      <div class="col-sm-7 form-input">
        <%= form.text_field :new_category_name, placeholder: "Enter New Category", oninput:"checkInput()" %>
      </div>
    </div>
    <% if product.created_at != product.updated_at %>
      <div class ="row mb-4">
        <div class="col-sm-5 align-right label">
          <%= form.label :updated_by, 'Modified by Form',  id:"product_updated_by"  %>
        </div>
        <div class="col-sm-7 form-input">
          <%= user_updated_identifier(product) %>
        </div>
      </div>
      <div class ="row mb-4">
        <div class="col-sm-5 align-right label">
          <%= form.label :updated_at, 'Modified on',  id:"product_updated_at"  %>
        </div>
        <div class="col-sm-7 form-input">
          <%= product.updated_at.strftime("%d-%b-%Y @ %H:%M")  %>
        </div>
      </div>
    <% end %>
    <div class="mt-5 mb-5 text-center">
      <% if action_name == 'edit' %>
        <%= form.submit 'Update Product', class: 'btn btn-submit', id:"myButton" %>
      <% else %>
        <%= form.submit class: 'btn btn-submit', id:"myButton" %>
      <% end %>
    <% end %>
  </div>
</div>

It looks as follows in the browser

enter image description here

The new and create actions in the products_controller are as follows:


  def new
    @product = @restaurant.products.new
    @categories = @restaurant.categories
  end


  def create
    @product = @restaurant.products.new(product_params)
  
    if @product.new_category_name.present?
      new_category = @restaurant.categories.find_or_create_by(name: @product.new_category_name)
      @product.category_id = new_category.id
    end
    
    @product.created_by = current_user.id
    puts "Product params AAAAA: #{product_params}"
  
    if @product.save
      redirect_to restaurant_products_path(@restaurant), notice: "Product was successfully created."
    else
      puts @product.errors.full_messages
      render :new
    end
  end

When I try to create the product without a name or a category I get the following error in the terminal:

14:52:45 web.1 | Category must exist

14:52:45 web.1 | Name can't be blank

14:52:45 web.1 | Category can't be blank

Which indicates that the else stament in the create action is being executed and the puts ( puts @product.errors.full_messages) is being executed. If the information is correct in the terminal why I can't see the errors in the view (browser)?

I have tried to sort it by myself but if has been a task of a day and counting...

3
  • 2
    stackoverflow.com/questions/71751952/… Commented Jun 19, 2024 at 14:48
  • Hi Alex, many thanks, much appreciated!. For any one who finds this issue in the future I added , data: { turbo: false } to the form, so it looks like: <%= form_with(model: [@restaurant, @product], data: { turbo: false }) do |form| %> the rest stays the ssme. All thanks to Alex. Commented Jun 19, 2024 at 15:03
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Jun 20, 2024 at 11:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.