简体   繁体   中英

How to hide form select option when an option is nil rails 4

I've got the following form select:

<%= f.select(:size_requested, [@item.size1, @item.size2, @item.size3, @item.size4, @item.size5, @item.size6, @item.size7, @item.size8, @item.size9, @item.size10], {}, { class: 'form-control quantity-select' }) %>

I'm trying to have it where only the sizes that are not nil are presented as options and avoid this:

在此输入图像描述

When I try things like (@item.size6 if @item.size6) for the options I still have the same issue and the nil options are still visible. Can anyone lead me in the right direction to solve this in a clean way?

Thanks for your help!

You can use the compact method to remove nils from an array. For example:

[1, nil, 2, nil].compact

Also, as a comment on your question suggests, it's a good idea to set up the array in your controller and then reference that in the view rather than performing logic in the view itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM