Ich habe eine E-Commerce-Website mit Produkten, Wagen und anderen Dingen.Schienen: Produkt ist auf Null gesetzt
Beide Variablen @produit und @produits sind auf Null gesetzt, aber ich habe keine Ahnung warum.
hier ist produit.rb
:
class Produit < ActiveRecord::Base
has_many :order_items
default_scope { where(active: true) }
mount_uploader :photo, PhotoUploader
validates :nom, presence: true
validates :artisan, presence: true
validates :quantite, presence: true
validates :categorie, presence: true
validates :denomination, presence: true
validates :description, presence: true
validates :prix, presence: true
validates :photo, presence: true
end
hier der Controller:
class ProduitsController < ApplicationController
def index
@produits = Produit.all
@order_item = current_order.order_items.new(produit_params)
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', notice: 'Le produit n\'a pas été ajouté au panier' }
format.json { render @order_item.errors, status: unprocessable_entity }
format.js
end
end
def show
@produit = Produit.find(params[:id])
end
private
def produit_params
params.require(:produit).permit(:nom, :quantite, :description, :denomination, :artisan, :categorie, :prix, :photo)
end
end
ich diese Ansicht auch in produits/Index:
<%= render partial: 'layouts/navbarindex' %>
<%= render partial: 'produits/header' %>
<div id="path-progress-bar">
<p>Accueil</p>
<p>/</p>
<p style="color:black">Boutique de produits breton</p>
</div>
<section id="marche-breton-container">
<%= render template: 'categories/index' %>
<div id="produits-column-container">
<% if @produits %>
<% @produits.in_groups_of(4, false).each do |g| %>
<% g.each do |produit| %>
<%= render partial: "produits/produit_row", produit: @produit, order_item: @order_item %>
<% end %>
<% end %>
<% end %>
</div>
</section>
und hier ist der Renderer _produit_row.html.erb
:
<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>
Das Problem ist, dass @produit
und @produits
auf Null gesetzt, so dass die Aussicht nichts anzeigt, obwohl ich eine active_admin Sitzung, wo Produkte eingerichtet werden.
UPDATE: die logs
:
Started GET "/mon-marche-breton" for ::1 at 2017-05-24 09:17:47 +0200
Processing by MarcheBretonController#index as HTML
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
Rendered layouts/_cart_text.html.erb (6.9ms)
Rendered layouts/_navbarindex.html.erb (26.4ms)
Rendered produits/_header.html.erb (3.4ms)
Categorie Load (0.3ms) SELECT `categories`.* FROM `categories`
Rendered categories/index.html.erb (5.5ms)
Rendered produits/index.html.erb (39.2ms)
Rendered marche_breton/index.html.erb within layouts/application (40.3ms)
Rendered layouts/_footer.html.erb (3.2ms)
Completed 200 OK in 253ms (Views: 242.1ms | ActiveRecord: 3.6ms)
Started GET "/assets/bg/bg-banner.png" for ::1 at 2017-05-24 09:17:48 +0200
Started GET "/assets/bg/footer.png" for ::1 at 2017-05-24 09:17:48 +0200
Started GET "/assets/bg/before-footer.png" for ::1 at 2017-05-24 09:17:48 +0200
Haben Sie in der Rails-Konsole eingecheckt, dass 'Produit.all' Daten bekommt. Außerdem haben die Produkte, die Sie gespeichert haben, das Attribut "active: true"? erinnere dich an das 'default_scope {where (active: true)}}'. –
'Produits # index' sollte keine Parameter für' OrderItem' erhalten. Es sollte eine GET-Anfrage sein. Ziehen Sie in Erwägung, die '@ order_item.save'-Bedingung an einen' OrderItems'-Controller zu übergeben (wahrscheinlich in einer 'create'-Aktion). – coreyward
@AlejandroMontilla Ja, ich habe die Rails-Konsole überprüft und 'Produit.all' erhält die Daten, die ich in meiner active_admin-Sitzung eingegeben habe. Deshalb verstehe ich nicht ... – justinedps26