user5914341
user5914341

Reputation:

Rails: form doesn't show up

I'm building a shopping cart and i have an issue with the following form:

<div id="produits-column-container">
<% if @produits %>
  <% @produits.in_groups_of(4, false).each do |g| %>
    <% g.each do |produit| %>
    <div id="produits-row-container">
      <div id="fiche-produit-container">
        <%= form_for order_item, remote: true do |f| %>
          <div id="produit-img">
            <%= link_to produits_show_path do %>
              <%= image_tag produit.photo %>
            <% end %>
          </div>
          <div id="produit-nom">
            <%= produit.nom %>
          </div>
          <div id="produit-prix">
            <%= number_to_currency(produit.prix, unit: '€', format: "%n%u") %>
          </div>
          <div id="produit-au-panier">
            <%= image_tag('icon/icon-panier') %>
            <%= f.submit 'Ajouter au panier' %>
          </div>
        <% end %>
      </div>
    </div>
    <% end %>
  <% end %>
<% end %>

When i run the server, the form doesn't appear in the view even though Rails doesn't raise any issue. I don't understand why, any idea?

EDIT

here is my produits_controller.rb :

class ProduitsController < ApplicationController

  def index
    @produits = Produit.all
    @order_item = current_order.order_items.new

    if @order_item.save
      format.html { redirect_to @order_item, notice: 'Le produit a été ajouté au panier !' }
      format.json { render json: @order_item, status: :created, location: @order_item }
      format.js
    else
      format.html { render action: "create" }
      format.json { render @order_item.errors, status: unprocessable_entity }
      format.js
    end
  end

  def show
    @produit = Produit.find(params[:id])
  end

end

Upvotes: 0

Views: 187

Answers (1)

Reuben Mallaby
Reuben Mallaby

Reputation: 5767

Remove the line with if @produits.

With that line in place, the form will not show when @produits is empty.

Upvotes: 1

Related Questions