开发者

Ruby on Rails Mobile Application

I am trying to develop a Ruby on Rails application that will detect the client i.e the mobile (browser) that connects to the server and render the appropriate layout. I tried using the following link but still not able to connect it. Any suggestions ?

http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens

I am using Opera Mini Emulator to test 开发者_如何学运维the application.


The most elegant solution for this I've seen is to do two things: recognize mobile users based on user-agent in the request and use a custom Rails mime type to respond with, allowing custom HTML templates for mobile users.

Define a custom 'mobile' Rails MIME type in config/initializers/mime_types.rb that's just HTML:

Mime::Type.register_alias "text/html", :mobile

Add a helper/filter to respond to mobile users:

def mobile_user_agent?
  @mobile_user_agent ||= ( request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/] )
end

then..

before_filter :handle_mobile

def handle_mobile
  request.format = :mobile if mobile_user_agent?
end

Make custom mobile template:

app/views/users/show.html.erb => app/views/users/show.mobile.erb

Dispatch mobile or regular in controllers:

respond_to do |format|
  format.html { }   # Regular stuff
  format.mobile { } # other stuff
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜