2017-05-23 8 views
0

제품, 수하물 및 기타 사항이있는 전자 상거래 웹 사이트가 있습니다.레일 : 제품이 없음으로 설정되었습니다.

@produit과 @produits 변수는 모두 nil로 설정되지만 이유는 알 수 없습니다. 여기

produit.rb이다 : 나는 또한/인덱스 PRODUITS에서이보기가

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 

: 여기

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 

컨트롤러 여기

<%= 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 세션에도 불구하고, 전무로 설정되어 있다는 점이다.

UPDATE는 다음 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 
+0

레일 콘솔에서 'Produit.all'이 데이터를 가져 오는 지 확인 했습니까? 또한, 저장 한 제품에는 'active : true' 속성이 있습니까? 'default_scope {where (active : true)}'를 기억하십시오. –

+2

'Produits # index'는'OrderItem'에 대한 매개 변수를 받아서는 안됩니다. GET 요청이어야합니다. '@ order_item.save' 조건을'OrderItems' 컨트롤러로 이동하는 것을 고려하십시오 (아마도'create' 액션에서). – coreyward

+0

@AlejandroMontilla 예 레일 콘솔을 확인했는데'Produit.all'이 active_admin 세션에 입력 한 데이터를 가져 왔습니다. 이것이 내가 이해하지 못하는 이유입니다 ... – justinedps26

답변

0

문제는 여기에 있습니다 :

<% @produits.in_groups_of(4, false).each do |g| %> 
     <% g.each do |produit| %> 
      <%= render partial: "produits/produit_row", produit: @produit, order_item: @order_item %> 

당신은 컬렉션 @produits을 반복하고 produit에 컬렉션의 각 단일 인스턴스를 할당합니다. 따라서 부분 produit: @produit을 전달하면 실제로는 nil을 전달합니다.

이 문제를 해결하려면 @을 제거하고 올바르게 범위를 지정하십시오! :)

<% @produits.in_groups_of(4, false).each do |g| %> 
     <% g.each do |produit| %> 
      <%= render partial: "produits/produit_row", produit: produit, order_item: @order_item %> 

그 외에 내가 당신의 부분에 produits_show_path 그런 식으로 작동하지 않습니다 가정합니다. 다음과 같이 설정하면됩니다.

link_to produit_path(produit) do 

이 정보가 도움이되기를 바랍니다.

+0

것은'@ produits'도 nil입니다. 나는 당신이 제안한 것을하려고 노력했지만 그것은 효과가 없습니다 ... – justinedps26

+0

오, 알았어. 먼저 DB에 Produit이 있는지 확인하십시오. 그 외에도 index-method에서'if @ order_item.save' 부분을 제거하고 (else 케이스도 제거하십시오) 인덱스 뷰를 렌더링하는지 확인하십시오. 이 조건부로 인해 실제로 원하는보기를 렌더링하지 못했을 것입니다. –

+0

'Produit'가 DB에 있습니다. 나는'if @ order_item.save' 문 (또한 else 문)을 제거하고 아무것도 변경하지 않습니다. 로그와 함께 질문을 업데이트하여 어떤 일이 일어나는지 보도록하겠습니다. – justinedps26

관련 문제