Search code examples
ruby-on-railspartialslim-lang

Rendering Partials with Slim


Current Slim.html:

#fullpage
  =render partial: 'section_one'
  =render partial: 'section_two'
  =render partial: 'section_three'
  =render partial: 'section_four'
  =render partial: 'section_five'

The issue I'm having is that I don't want to render partial: 'section_four' is this is being viewed on mobile.

I tried this:

#fullpage
  =render partial: 'section_one'
  =render partial: 'section_two'
  =render partial: 'section_three'
  - if $(window).width() >= 700
    =render partial: 'section_four'
  =render partial: 'section_five'

But it doesn't work. Any ideas?


Solution

  • Try to use request.user_agent to recognize mobile device. Also you should use inline if condition while checking for mobile device:

    = render partial: 'section_four' if request.user_agent =~ /Mobile|webOS/
    

    There can be some cases where request.user_agent =~ /Mobile|webOS/ will fail to return expected results due to browser's custom user_agent.

    So, I recommend Mobile-Fu to work with it in better way.