foxtrotuniform6969
foxtrotuniform6969

Reputation: 4125

Controller Action Based on Media Query (Rails)

I have a specialized dashboard for my Rails application that is shown to users using a media query. I'd like to have devise redirect users on mobile devices (through media queries) to that specific dashboard upon sign in.

What's a good way to get this done, while attempting to avoid having rails actually detect anything? I've looked into gems like browser a bit, but I'd like something a little more comprehensive and customizable, such as a media query.

Any suggestions of places to start looking, or what kind of code I could put in my View / JS / CSS to make this happen?

I'd highly prefer to fire off a specific controller action for specific media queries, but I feel like that may be an unachievable dream.

Upvotes: 0

Views: 380

Answers (1)

Rodrigo
Rodrigo

Reputation: 4802

I think you can use Variants to achieve this.

With variant option, you can do something like this to render different dashboard views :

respond_to do |format|
  format.html do |html|
    html.desktop # renders app/views/projects/show.html+desktop.erb
    html.mobile { extra_setup; render ... }
  end
end

Or, you can develop mobile friendly views for all actions and do this (using browser gem):

before_action :define_variant

def define_variant
  request.variant = :tablet if browser.phone?
end

Upvotes: 1

Related Questions