ruby-on-railsslim-lang

how to conditionally add a div in slim


I have a small html fragment and want to write a conditional such that the first two loading results are wrapped in a div called "loading-top-two" but not sure how to do it. Was thinking something like this but (obviously) doesn't work. How would I do this? On RoR, if there's any magic to be had there.

.loader-results
  - (0..2).each do |i|
    - if i==0
      .loading-top-two
        = render "lawyers/search/loading_result", idx: i
    - else
      = render "lawyers/search/loading_result", idx: i

Solution

  • If I understand correctly you can just do this, first render the first two and then the rest of the result. [2...] means everything from the item with index = 2 until the end (infinity)

    .loading-top-two
      - @result[0..2].each_with_index do |result, index|
        = render "lawyers/search/loading_result", idx: index
    
    - @result[2...].each_with_index do |result, index|
      = render "lawyers/search/loading_result", idx: index