ruby-on-rails - how to show a specific information from a string of User Agents string saved into my database
问题描述
I am working with Geolocation and I have a string of User Agent saved into my database as shown below:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
In this case of the string above, the information I would like to output in my views is Macintosh; Intel Mac OS X 10_13_6
. So, in the frontend/views, I don't want to output the whole data as its in the database. All I want to pick from this information is just to show the devise the user used to login into the application, and I do not know how I will show only what I want. Any help or pointer to what I should do will be appreciated.
view
.row
.panel.panel-primary
.panel-heading
span = t('admin_header.traffics')
.panel-body
= table_for(@traffic, class: 'table table-condensed table-hover') do |t|
- t.column :ua, 'Device Used', class: 'col-xs-1' # But this shows all the string which I do not want, I only want specific details from it.
Here is the code that saves User Agent string into the Database:
def save_signup_history(member_id)
SignupHistory.create(
member_id: member_id,
email: @member.email,
ip: request.remote_ip,
accept_language: request.headers["Accept-Language"],
ua: request.headers["User-Agent"], #Here is User Agent
login_location: get_ip_location
)
end
The only thing I can think of is to use .remove
method, but I don't think its a best solution to my problem.
解决方案
How about using the user_agent gem?
In the specific example you gave you could use:
user_agent = request.env['HTTP_USER_AGENT']
user_agent.match(/\(.*?\)/)[0]
However that may not cover every case, and either using a gem or code which accounts for the various options is your best bet.
推荐阅读
- html - Bootstrap flexbox,2 行,1 列布局和对齐内容
- haskell - 一种相互连接的函数的类型
- python - 在熊猫数据框中查找给定条件的最新值
- yii2 - Yii2:在没有网络服务器帮助的情况下重写 URL
- xamarin.forms - 我们如何使用认知面部 API 服务检查任何图像的活跃度?
- python - 从 S3 读取 netcdf4 文件
- python - 从文件中打印单行并在末尾删除多余的行
- r - 使用 R 中的查找表对字符串中的字符求和
- javascript - 在 index.html 的脚本标签中添加脚本标签
- python - 如何从列表中获取元素对的总和