簡體   English   中英

如何顯示保存在我的數據庫中的用戶代理字符串中的特定信息

[英]how to show a specific information from a string of User Agents string saved into my database

我正在使用Geolocation,並且將用戶代理字符串保存到數據庫中,如下所示:

Mozilla / 5.0(Macintosh; Intel Mac OS X 10_13_6)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 68.0.3440.106 Safari / 537.36

在上面的字符串的情況下,我想在視圖中輸出的信息是Macintosh; Intel Mac OS X 10_13_6 Macintosh; Intel Mac OS X 10_13_6 因此,在前端/意見,我不想輸出數據庫中的全部數據作為其。 我要從此信息中選擇的只是顯示用戶登錄應用程序所用的設計,而我不知道如何僅顯示我想要的內容。 對我應該做的任何幫助或指示,將不勝感激。

視圖

.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.

以下是將用戶代理字符串保存到數據庫中的代碼:

  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

我唯一能想到的就是使用.remove方法,但是我認為這不是解決我的問題的最佳方法。

如何使用user_agent gem

在您提供的特定示例中,您可以使用:

user_agent = request.env['HTTP_USER_AGENT']
user_agent.match(/\(.*?\)/)[0]

但是,這可能無法涵蓋所有​​情況,因此使用gem代碼說明各種選擇是您的最佳選擇。

您可以使用scan方法獲取所需的字符串。 例:

user_agent = 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

然后,

user_agent.scan(/Mac.*_6/)

應該給你你想要的字符串。 您可以根據需要對其進行修改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM