我有一個包含產品,購物車和其他東西的電子商務網站。Rails:產品設置爲零
變量@produit和@produits都設置爲零,但我不知道爲什麼。
這裏是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
這裏是控制器:
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
我也有在PRODUITS /索引這樣的觀點:
<%= 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>
,這裏是渲染器_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>
的問題是,@produit
和@produits
設置爲零,所以視圖不顯示任何東西,即使我有哪裏的產品建立一個active_admin會話。
更新: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
您是否在軌道控制檯中檢查過'Produit.all'正在獲取數據。另外,您存儲的產品具有「active:true」屬性?請記住'default_scope {where(active:true)}'。 –
'產品#索引'不應該接收'OrderItem'的參數。它應該是一個GET請求。考慮將'@ order_item.save'條件移動到'OrderItems'控制器(可能在'create'動作中)。 – coreyward
@AlejandroMontilla是的,我沒有檢查軌道控制檯和'Produit.all'獲取我在我的active_admin會話中輸入的數據。這就是爲什麼我不明白... – justinedps26