同じものが必要です。私はこのスタックオーバーフローの質問(および他の同様の質問)を含めてこれを調査し、https://github.com/rails/rails/issues/3855でrailsスレッド(この質問で言及されている)をフォローし、そのスレッドをフォローしました/要旨/宝石。
Rails3.1とエンジンで動作するように私がやったことは次のとおりです。このソリューションを使用すると、*。mobile.haml(または* .mobile.erbなど)を他のビューファイルと同じ場所に配置できます。2つの階層(通常用とモバイル用)は必要ありません。
エンジンと準備コード
私の「ベース」エンジンでこれを追加しましたconfig/initializers/resolvers.rb
:
module Resolvers
# this resolver graciously shared by jdelStrother at
# https://github.com/rails/rails/issues/3855#issuecomment-5028260
class MobileFallbackResolver < ::ActionView::FileSystemResolver
def find_templates(name, prefix, partial, details)
if details[:formats] == [:mobile]
# Add a fallback for html, for the case where, eg, 'index.html.haml' exists, but not 'index.mobile.haml'
details = details.dup
details[:formats] = [:mobile, :html]
end
super
end
end
end
ActiveSupport.on_load(:action_controller) do
tmp_view_paths = view_paths.dup # avoid endless loop as append_view_path modifies view_paths
tmp_view_paths.each do |path|
append_view_path(Resolvers::MobileFallbackResolver.new(path.to_s))
end
end
次に、「ベース」エンジンのアプリケーションコントローラーにモバイルを追加しましたか?方法:
def mobile?
request.user_agent && request.user_agent.downcase =~ /mobile|iphone|webos|android|blackberry|midp|cldc/ && request.user_agent.downcase !~ /ipad/
end
そしてこれもbefore_filter
:
before_filter :set_layout
def set_layout
request.format = :mobile if mobile?
end
最後に、これをconfig/initializers/mime_types.rb
:に追加しました
Mime::Type.register_alias "text/html", :mobile
使用法
今、私は(私のアプリケーションレベルで、またはエンジンで)持つことができます:
app/views/layouts/application.mobile.haml
- そして、どのビューでも、ファイルの
.mobile.haml
代わりに。.html.haml
コントローラーに設定すれば、特定のモバイルレイアウトを使用することもできます:layout'mobile'
を使用しますapp/views/layouts/mobile.html.haml
(またはmobile.mobile.haml
)。