Rails Browser Detection Methods

Rails Browser Detection Methods

There's library ruby library over at GitHub: https://github.com/gshutler/useragent

I use it myself, and it's working as advertised so far. For your use case, you could probably call the library from within a helper method in your Rails project or something similar.

That said, I'm not completely sure if the HTTP_USER_AGENT is exposed to Rails helper methods. In case it isn't exposed, you could always expose a controller method as a helper (by using AbstractController::Helpers::ClassMethods#helper_method).

How to detect browser type and its version

Try the browser gem. Very simple and can solve your purpose.

You can find the gem Here very easy to use.

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

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

request.user_agent =~ /Mobile|webOS/

Rails: Run a method when user left the edit view through the browser back button

Hitting the back button in a web browser won't involve Rails, but rendering the component to which it is being sent will.

You can save the data of the input fields in the browser's local storage or cookies and then have a before_action: in the controller they were sent to with the back button that looks for the presence of such value.
( This solution involves some javascript )

Then, you can query accordingly to determine if those values should be saved in the database.

Note: If you need to cover not only the back button scenario, but also any other controller to which the user might navigate to in your app, you may add the before_action to somewhere else, like application_controller.

Rails way to detect mobile device?

You can do it that way by defining a function like below:

def mobile_device?
if session[:mobile_param]
session[:mobile_param] == "1"
else
request.user_agent =~ /Mobile|webOS/
end
end

Or you can use gems to detect mobile devices like

  • https://github.com/tscolari/mobylette
  • https://github.com/shenoudab/active_device


Related Topics



Leave a reply



Submit