4

この質問は、以前の未回答の質問のフォローアップです:ActionView :: MissingTemplate:テンプレートがありません(存在しない:mobile形式をレンダリングしようとしています)

Railsのアプローチはこれとのコンセンサスではないようですが、モバイルデバイスからアクセスして、フォーマットが利用できない:htmlときにデフォルトをレンダリングする方法はありますか?:mobile:mobileビューが存在する場合は、モバイル形式でないものよりも優先する必要があります)。

4

3 に答える 3

3

モバイルリクエストを検出するためのコントローラーインスタンスメソッドがあると仮定するとmobile_request?、フォーマットフォールバックチェーンを設定できるはずです。

# application_controller.rb
before_filter :set_request_format, :set_format_fallbacks

respond_to :html, :mobile # etc

def set_request_format
  request.format = :mobile if mobile_request?
end

def set_format_fallbacks
  if request.format == :mobile
    self.formats = [:mobile, :html]
  end
end

これは機能するはずですが、完全には機能しないようです。 https://github.com/rails/rails/issues/3855 モバイルテンプレートを使用している場合、フォーマットがロックされているように見え、htmlのみのパーシャルは見つかりません。

うまくいけば、それは何らかの方法で修正されるでしょう。それまでの間、この<%controller.set_format_fallbacks%>を各テンプレート(ouch)に配置するか、独自のリゾルバーを作成できます。 http://jkfill.com/2011/03/11/implementing-a-rails-3-view-resolver/

また見てください:

モバイルmimeタイプはRailsの「html」にフォールバックできますか?

Rails 3.1でのビュー形式の変更(モバイルHTML形式の配信、通常のHTMLへのフォールバック)

于 2012-05-22T18:16:42.443 に答える
0

これを試して:

if File.exists?('app/views/object/mobile.file.erb')
  render :mobile
else
  render :html
end
于 2012-05-22T17:31:33.423 に答える
0

同じものが必要です。私はこのスタックオーバーフローの質問(および他の同様の質問)を含めてこれを調査し、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)。

于 2013-03-31T17:57:56.997 に答える