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
Iterate Through Every File in One Directory
Difference Between $Stdout and Stdout in Ruby
What Is a Regex to Match a String Not At the End of a Line
Can't Access Rubygems - Possibly Due to Ssl
Rails Sends 0 Byte Files Using Send_File
Recommended Development Web Server for Ruby on Rails 3
Rails Dot Instead of Slash in Url
What's the 'Ruby Way' to Iterate Over Two Arrays at Once
Dynamic Class Definition with a Class Name
Undefined Method Raise_In_Transactional_Callbacks=' for Activerecord::Base:Class (Nomethoderror)
Read Binary File as String in Ruby
How to Stub Things in Minitest
How to Define Action With Simple Form For
What Does "≪≪-" Mean in Ruby
Why Doesn't My Cron Job Work Properly
Ruby Array Subtraction Without Removing Items More Than Once