I’m using Ruby on Rails and I want to display a drop down list in a view, but only if items in the list exist. What is a good way to check for the presence of at least one instance of a model, and then display in the view based on this condition?
I was thinking of using an if statement such as:
if firstmodel.secondmodels
. . html stuff
But I think the empty array doesn’t stop evaluate to false.
You could use something like this in your view:
Generally you should try to keep logic out of your view, but I think in this simple case there are not many alternatives (?)
If there are too many
if elselike that your view, well… you could maybe check for this in your controller and render another view altogether, like: