Domanda

Sto usando il gemma mobile FU per eseguire un rilevamento dell'agente utente, in modo che io possa servire. I modelli di estensione .html o .mobile prolunga a seconda del client,

Ora, questa parte funziona davvero bene, ma non mi piace che le cartelle di visualizzazione diventino un po 'ingombrate con il doppio dei file, cioè.

app/views/profiles/show.mobile.haml

app/views/profiles/show.html.haml

app/views/profiles/edit.mobile.haml

app/views/profiles/edit.html.haml

ecc, ecc

Cosa mi piacerebbe avere invece:

app/views/profiles/html/show.html.haml

app/views/profiles/html/edit.html.haml

e

app/views/profiles/mobile/show.mobile.haml

app/views/profiles/mobile/edit.mobile.haml

e hanno guidato automaticamente nella cartella / directory corretta per i file a seconda della richiesta. è possibile fare?

Forse questo è qualcosa di veramente facile da fare, fammi sapere se questo è un comportamento che esce dalla scatola ..

Grazie

È stato utile?

Soluzione

Rails 4.1 ha una nuova funzionalità integrata denominata Varianti d'azionePack , il che rileva l'utente-agente (come il gemma mobile Fu).

Fondamentalmente, è possibile aggiungere questo ad esempio nel tuo ApplicationController:

before_action :detect_device_format

private

def detect_device_format
  case request.user_agent
  when /iPad/i
    request.variant = :tablet
  when /iPhone/i
    request.variant = :phone
  when /Android/i && /mobile/i
    request.variant = :phone
  when /Android/i
    request.variant = :tablet
  when /Windows Phone/i
    request.variant = :phone
  end
end
.

Diciamo che hai un profilescontroller.Ora puoi farlo:

class ProfilesController < ApplicationController
  def index
    respond_to do |format|
      format.html          # /app/views/profiles/index.html.erb
      format.html.phone    # /app/views/profiles/index.html+phone.erb
      format.html.tablet   # /app/views/profiles/index.html+tablet.erb
    end
  end
end
.

Torna alla tua domanda: Se vuoi cercare i file in una cartella / directory diversa, puoi farlo:

format.html.phone { render 'mobile/index' }   # /app/views/mobile/index.html+phone.erb
.

C'è anche un buon tutorial che mostraCome usarlo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top