LoginSignup
1
2

More than 5 years have passed since last update.

Jpmobileでビュー自動切り替えのon/off

Last updated at Posted at 2014-10-16

まだプログラム作ってないけど、これでできそう(な気がする)
ちなみにrails 3.0、jpmobileは1.0。
 
 


目的

タブレットで見るときPCとかスマホで見たい。
(任意で切り替えたい)
 

こんな感じでやる

まだ書いてないから動作確認してないヨ
if request.mobile.tablet?
  register_mobile if [onにする条件]
  disable_mobile_view! if [offにする条件] 
end

これをビューをsp_controller.rb辺りに仕込む。
上のメソッドはjpmobileに組み込まれてるもので、

  • register_mobile:自動機能on
  • disable_mobile_view!:自動機能off

(ちなみにReadmeにはdisable_mobile_view!しか書いてない)

 


分岐の条件

分岐の条件はredisで実施する予定。

ビューにjavascriptで[PC/スマホ]を表示切替ボタンを作り、
ボタンが押されたらredisにon/off条件となるようなフラグをセット。
そのフラグによって上に書いたみたいなプログラムが自動判定する。
※そのフラグがない場合はデフォルト、みたいな一文があってもいいかも。

まだ書いてないから動作確認してないヨ・改変
def ナントカー
  if [redisに値入ってない?] //1回通れば(ほぼ)通らないはず。
    [redisに値入れる処理(onにする条件をセット)]
    register_mobile
  end
  if request.mobile.tablet?
    register_mobile if [onにする条件]
    disable_mobile_view! if [offにする条件] 
  end
end

みたいな?


最終的にこれ使った

これ
prepend_view_path(Rails.root + 'app/views' + path)

pathに条件ごとにroot以下のディレクトリを入れた。
これをビュー表示時に設定するようにして対応。
Rails3でも動いたのでそのまま使ってます。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2