2014-02-24 1 views
0
field_for

에 한 번 나타날 내가 현재 가지고 것은 :갖는 라벨은

<%= f.label :attachment_uploader, 'Current Attachments:' %> 
<%= f.fields_for :data_files do |attachment| %> 
    <% if !attachment.object.new_record? %> 
     <%= attachment.label :attachment_uploader, 'Delete: ' + attachment.object.attachment_uploader_url.split("/").last %> 
     <%= attachment.check_box :_destroy %> 
    <% end %> 
<% end %> 

그러나, 나는 첨부 파일이없는 경우 레이블이 여전히 존재한다. 미학을 위해서 첨부 파일이 없으면 숨기고 싶습니다.

나는 유사한 무언가를 생각하고 있었다

:

<%= f.fields_for :data_files do |attachment, index| %> 
    <% if index == 0 %> 
    <%= attachment.label :attachment_uploader, 'Current Attachments:' %> 
    <% end %> 
    #rest of code 
<% end %> 

하지만 그건 작동하지 않습니다!

나는 다른 게시물에서 f.options[:index]에 대해 읽었지만 알아 내지 못했습니다.

답변

1

조건을 fields_for 앞에 추가하십시오. @object가 양식을 만든 객체입니다.

<%= f.label :attachment_uploader, 'Current Attachments:' %> 
<% unless @object.data_files.empty? %> 
    <%= f.fields_for :data_files do |attachment| %> 
     <% if !attachment.object.new_record? %> 
      <%= attachment.label :attachment_uploader, 'Delete: ' + attachment.object.attachment_uploader_url.split("/").last %> 
      <%= attachment.check_box :_destroy %> 
     <% end %> 
    <% end %> 
<% end %> 
+0

고마워요! 이전에 시도했지만 .emil 대신'.nil? '을 사용했습니다. 어리석은 실수. – Darkstarone