2013-03-24 2 views
0

내 모델 색인 페이지에서 이상한 동작이 나타납니다. 하나의 모델 객체를 만들면 인덱스 페이지에 올바르게 표시됩니다. 나는 두 번째 모델 객체를 생성 할 때 중복 된 개체가 내 데이터베이스에서 생성되지 않는 것을 제가 확인했습니다 그래서모델 index.html 중복 된 데이터를 표시하는 페이지

OBJECT A 
OBJECT B 
OBJECT A 
OBJECT B 

처럼, 인덱스 페이지에 두 개체의 중복을 보여줍니다. 또한 OBJECT B를 파괴하면 OBJECT A가 올바르게 표시됩니다.

index.html.erb

<table class="table"> 
    <thead> 
    <tr> 
     <th>Image</th> 
     <th>Name</th> 
     <th>Description</th> 
     <th>URL</th> 
     <th></th> 
     <th></th> 
     <th></th> 
    </tr> 
    </thead> 

    <tbody> 
    <%= render @companies %> 
    </tbody> 
</table> 

_company.html.erb

<% @companies.each do |company| %> 
    <tr> 
    <td><%= image_tag company.image(:medium) %></td> 
    <td><%= company.name %></td> 
    <td><%= company.description %></td> 
    <td><%= company.url %></td> 
    <td><%= link_to 'Show', company %></td> 
    <td><%= link_to 'Edit', edit_company_path(company) %></td> 
    <td><%= link_to 'Destroy', company, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
<% end %> 

는 companies_controller.rb

def index 
    @companies = Company.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @companies } 
    end 
    end 
+0

여기에 링크 된 부분의 이름은 무엇입니까? – drewinglis

+0

_company.html.erb – mnort9

+0

'index' 액션에 대한 컨트롤러 코드를 게시하십시오. 어쩌면 예제에서 HTML을 다듬을 수도 있습니다. ''행은 중요하지 않습니다. –

답변

2

은 변경 당신의 부분에,

<tr> 
    <td><%= image_tag company.image(:medium) %></td> 
    <td><%= company.name %></td> 
    <td><%= company.description %></td> 
    <td><%= company.url %></td> 
    <td><%= link_to 'Show', company %></td> 
    <td><%= link_to 'Edit', edit_company_path(company) %></td> 
    <td><%= link_to 'Destroy', company, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
</tr> 

부분적으로 각 루프를 삭제해야합니다.

<%= render @companies %>은 각 회사에 대해 부분을 렌더링하지만 각 부분에서 다시 회사를 반복합니다.

더 많은 정보를 원하시면 http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-collections에서 3.4.5 렌더링 컬렉션을를 참조

1

변경 <%= render @companies %><%= render "company" %>에; 귀하의 부분은 여러 번, 각 회사에 하나씩 렌더링되며, 귀하의 부분은 모든 회사를 렌더링합니다. 이렇게하면 부분 만 렌더링되어 모든 회사가 렌더링됩니다. 원하는 부분 만 렌더링됩니다.

관련 문제