2016-08-23 49 views
0

這是一個index.html.erb。如何在備註屬性列中不存在記錄的情況下使用方法隱藏表頭,如備註?最好不使用JavaScript。如果沒有列屬性,就隱藏表頭 - Ruby on rails

index.html.erb

<table id = "kola" class="table listing text-center"> 
       <% has_remark = collection_has_remark?(@aslani361s) %> 
       <thead> 
        <tr class="tr-head"> 
         <td>Date</td> 
         <td>Description</td> 
         <td>Amount</td> 
         <td>Discount</td> 
         <td>Paid</td> 
         <td>Balance</td> 
         <td>DelnDel</td> 
         <% if has_remark %> 
         <td>Remark</td> 
         <% end %> 
         <td>Hide</td> 
        </tr> 
       </thead> 
</table> 

但是我能夠隱藏備註屬性值類似如下;

_aslani361.html.erb

<% if aslani361.remark.present? -%> 
    <td class="col-1"><%= aslani361.remark %></td> 
<% end %> 

aslani361s_helper.rb

module Aslani361sHelper 
    def collection_has_remark?(collection) 
     collection.each do |aslani361| 
      if aslani361.remark.present? 
       return true 
      end 
     end 
    end 
end 

aslani361.rb

class Aslani361 < ActiveRecord::Base 

end 

任何建議者居多。

預先感謝您。

+0

難道你不能做同樣的事情,你做隱藏的屬性值?只需在if語句中包裝Remark標題 – davidhu2000

+0

你可以發佈你的模型嗎?如果不知道備註的存儲位置,它們與其他模型的關係如何,幾乎不可能拿出用來檢查整列的代碼。 – jaydel

+0

感謝您的回覆。如何使用if語句來包裝Remark標題? –

回答

1

如果你想隱藏的列,因爲你的數組中沒有記錄有句話值,你可以做這樣的事情:

定義您幫助模塊文件的方法的控制器:

def collection_has_remark?(collection) 
    collection.each do |record| 
    if record.remark.preset? 
     return true 
    end 
    end 
end 

然後用它在視圖

<% has_remark = collection_has_remark?(@records) %> 

<thead> 
    <tr class="tr-head"> 
     <td>Date</td> 
     <td>Description</td> 
     <td>Amount</td> 
     <td>Discount</td> 
     <td>Paid</td> 
     <td>Balance</td> 
     <% if has_remark %> 
      <td>Remark</td> 
     <% end %> 
    </tr> 
</thead> 

然後使用if語句相同的循環中。就我個人而言,我認爲留下一個空欄是很重要的,所以用戶肯定知道它沒有。

+0

謝謝你的回覆。不幸的是,我得到這個錯誤未定義的方法'collection_has_remark'爲#<#:0x00000007501ed0>。 –

+0

我更新了我的帖子,請看看和指導。 –

+0

這是我的一個錯字,對不起。添加?在視圖中方法名稱的末尾。應該是:'collection_has_remark?(@ records)'。更新我的回答 – agmcleod