is there a “rails” way to redirect if mobile browser is detected?

不问归期 提交于 2019-12-10 02:43:30

问题


I want to run a user-agent check but only on my homepage (static_controller#home) I have taken a close look at the code: http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens and it seems close but it never gets tied together, its just method creation... I am open to a jquery method but would prefer ruby/rails over js.

I only really care about iphone and droid...


回答1:


Check out this screencast. They suggest using the following for detecting a mobile device:

request.user_agent =~ /Mobile|webOS/



回答2:


http://detectmobilebrowsers.com/

Extremely useful for me.




回答3:


From this SO answer:

The best way is to use some supported plugin/gem, like browser

This is much better than writing your own regex.

For example Opera 11.50 has the following user_agent:

Opera/9.80 (Android 2.3.7; Linux; Opera Mobi/ADR-1111021303; U; en-GB) Presto/2.9.201 Version/11.50

The suggested regex of:

request.user_agent =~ /Mobile|webOS/

would total miss that.

You also get the benefit of knowing what platform (mac, windows etc), tablets, ie6 etc. I am a rookie rails dev, I set it up and got it working in like 2 minutes. Highly recommended.




回答4:


Try this: https://github.com/neighborland/mobu

Mobu provides a Rails controller concern called DetectMobile. Mobu does server-side User Agent detection to categorize requests as mobile, tablet, or default.

Mobu modifies your rails view paths based on the request type. It does not require custom MIME types or separate subdomains.



来源:https://stackoverflow.com/questions/6905765/is-there-a-rails-way-to-redirect-if-mobile-browser-is-detected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!