Search code examples
ruby-on-railsslim-lang

Rails conditionally apply class pending answer with Slim


I've never done a conditional apply of a class so I'm at a bit of a loss.

- if @something.elses.all.count > 5
 .this-client
  - @something.elses.all.each do |thing|
   = image_tag thing.photo, class: 'align-middle'
- else
 .this-other-client
  - @something.elses.all.each do |thing|
   = image_tag thing.photo, class: 'align-middle'

RuboCop really hates that I have the same thing in both conditions and I get that. So how do I apply a conditional class based on the results?


Solution

  • You can do:

    div class=(@something.elses.all.count > 5 ? 'this-client' : 'this-other-client')
      - @something.elses.all.each do |thing|
       = image_tag thing.photo, class: 'align-middle'
    

    Also, it's un-Rails-like to perform a query like that in a view. If there's another way to get that count you should look into it