2014-07-01 3 views
0

다음은 저에게 계정 목록을 제공하는 코드입니다.계정에서 프로젝트 목록에 액세스하기

<% @accounts.each do |account| %> 
    <li><%= link_to account.name %></li> 
<% end %> 

내 계정의 프로젝트 목록에 액세스하는 방법.

+2

시도의 account.projects : 당신은 같은 뜻이

  • <% = LINK_TO account.projects %>
  • Mandeep

    +0

    @Mandeep 내부 (210)

    <% @accounts.each do |account| %> <li><%= link_to account.name %></li> <% if account.projects %> <% account.projects.each do |project| %> <li><%= link_to project %></li> #this will give you individual project associated with that account <% end %> <% else %> # add this else block to execute your code when there are on projects <p> No projects associated with your account</p> <% end %> <% end %> 
    theJava

    답변

    2

    이 시도 : Incase의 당신을

    :

    <% @accounts.each do |account| %> 
        <li><%= link_to account.name %></li> 
        <% account.projects.each do |project| %> 
        <li><%= link_to project %></li> #this will give you individual project associated with that account 
        <% end %> 
    <% end %> 
    

    편집 : 각 개별 프로젝트에 대한 LINK_TO를 원하는 경우

    <% @accounts.each do |account| %> 
        <li><%= link_to account.name %></li> 
        <li><%= link_to account.projects %></li> #this will give you a collection of projects associated with that account 
    <% end %> 
    

    는 다음과 같은 또 다른 루프를 사용해야합니다 계정에 대한 프로젝트가 없습니다.

    루프
    +0

    왜 내가 정의되지 않은 메서드'전무 각 '점점 오전 다음 account.projects에 NilClass을 ? – theJava

    +0

    @theJava 계정에 대한 프로젝트가 없기 때문에 오류가 발생합니다. 프로젝트가없는 경우 내 대답 업데이트 – Mandeep

    +0

    프로젝트가없는 경우 일부 메시지를 표시 할 수 있습니까? 우리는 어떻게 그 레일을 할 것인가? – theJava

    관련 문제