2016-08-23 36 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