2010-11-23 3 views
4

두 모델 (Order 및 Product) 간에는 많은 관계가 있습니다. Lines라는 조인 테이블이 있으므로 사용자는 주문하려는 제품에 수량을 추가 할 수 있습니다. 내 index.html.erb 경우 성공적으로 인덱스 (주문/아이디/제품)를 갈 수했습니다레일 3 - 중첩 된 리소스에 대한 인덱스 뷰

resources :orders do 
    resources :products, :controller => "products"  
    end 
end 

: 내 경로는 다음과 같이 있도록

나는 주문 중첩 제품을 그냥 자리 표시 자, 그러나 데이터를 표시하려고 할 때 문제가 있습니다. 밖으로 erroring되는

내 제품 테이블합니다 (<%의 @의 products.each에 ... 라인)처럼 보이는 다음

def index 
    @order = Order.find(params[:order_id]) 
    @products = Product.all  


    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @products } 
    end 
    end 
:

<table> 
    <tr> 
    <th>URL</th> 
    <th></th> 
    <th></th> 
    <th></th> 
    </tr> 

<% @products.each do |product| %> 
    <tr> 
    <td><%= product.url %></td> 
    <td><%= link_to 'Show', product %></td> 
    <td><%= link_to 'Edit', edit_order_products_path(product) %></td> 
    <td><%= link_to 'Destroy', order, :confirm => 'Are you sure?', :method => :delete %></td> 
    </tr> 
<% end %> 

내 인덱스 방법은 다음과 같습니다

내 @products 개체가 nil임을 나타내는 오류가 발생했습니다. 그러나 콘솔 Product.all 4 항목을 반환합니다.

저는 newb입니다. 이것은 처음으로 중첩 된 리소스를 참조합니다. 인스턴스 변수 @products를 사용하여 잘못 호출했을 가능성이 있습니까?

감사

답변

3

1) 당신이 당신의 데이터베이스에 어떤 제품이 있습니까? @products.present?

<% if @products.present? %> 
    <% @products.each do |product| %> 
    <tr> 
    <td><%= product.url %></td> 
    <td><%= link_to 'Show', product %></td> 
    <td><%= link_to 'Edit', edit_order_products_path(product) %></td> 
    <td><%= link_to 'Destroy', order, :confirm => 'Are you sure?', :method => :delete %></td> 
    </tr> 
<% end %> 
<% else %> 
<tr> 
    <td colspan=4>You don't have any products yet.</td> 
</tr> 
<% end %> 

2 난 당신이이 순서에서만 제품을 게재하려는 가정) : 어떤 사용하는 경우 그것은 확인하는 것이 좋습니다. 당신이 경우에, 당신은 작성해야 :

@products = @order.products 

대신

@products = Product.all 
관련 문제