レールを探すようにしてください。サブディレクトリ内のモバイルファイル?
-
20-12-2019 - |
質問
私はモバイルfu gemを使用してユーザーエージェントの検出を行い、どちらかを提供できるようにしています。htmlまたは。クライアントに応じたモバイル拡張テンプレート,
さて、この部分は本当にうまくいきますが、ビューフォルダが2倍のファイル、つまり少し雑然としているのは好きではありません。
app/views/profiles/show.mobile.haml
app/views/profiles/show.html.haml
app/views/profiles/edit.mobile.haml
app/views/profiles/edit.html.haml
等、等
私が代わりに持っていたいのは:
app/views/profiles/html/show.html.haml
app/views/profiles/html/edit.html.haml
と
app/views/profiles/mobile/show.mobile.haml
app/views/profiles/mobile/edit.mobile.haml
また、リクエストに応じて、Railsがファイルの正しいフォルダ/ディレクトリを自動的に調べます。これは可能ですか?
たぶんこれは本当に簡単なことですが、これが箱から出てくる動作であるかどうかを教えてください。.
お疲れさまでした。
解決
Rails4.1には、次のような新しい組み込み機能があります アクションパックバリアント, 、これはユーザーエージェントを検出します(モバイルfu gemのように)。
基本的には、たとえば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
ProfilesControllerがあるとしましょう。今、あなたはこれを行うことができます:
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
あなたの質問に戻る:別のフォルダ/ディレクトリ内のファイルを探したい場合は、これを行うことができます:
format.html.phone { render 'mobile/index' } # /app/views/mobile/index.html+phone.erb
また、あります 良いチュートリアル それを使用する方法を示しています。