开发者

Can I check object is nill in Rails3 view?

开发者 https://www.devze.com 2023-02-05 21:39 出处:网络
I\'m trying to display a message if no transactions found. For that, I thought in the view I could use something like <% if @current_account.transactions.nill? %> puts \"No transactions yet\" &l

I'm trying to display a message if no transactions found. For that, I thought in the view I could use something like <% if @current_account.transactions.nill? %> puts "No transactions yet" <% end %> unf开发者_JAVA技巧ortunately this is not valid.

Any suggestion?


The code should be

<% if @current_account.transactions.nil? %>

and not

<% if @current_account.transactions.nill? %>

Thanks....


transactions is an array so @current_account.transactions will return an empty array which is not nil?.my suggestion is to use

    @current_acccount.transactions.empty?

check

  irb(main):011:0> u=User.new=> #<User id: nil, email: "",....
  irb(main):012:0> u.companies=>[]
  irb(main):013:0> u.companies.nil?=>false
  irb(main):015:0> u.companies.empty?=>true
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号