简体   繁体   English

Rails to_s机械师

[英]Rails to_s Mechanics

Hey guys this has been tripping me up quite a bit. 大家好,这让我很烦。 So here is the general problem: 所以这是一个普遍的问题:

I am writing an application that requires users to enter their Summoner Names from league of legends. 我正在编写一个应用程序,要求用户输入来自传奇联盟的召唤者名称。 I do a pretty simple data scrape of a match and enter the data into my database. 我对比赛进行了非常简单的数据刮刮,然后将数据输入到数据库中。 Unfortunately I am having some errors registering users with "special characters". 不幸的是,我在用“特殊字符”注册用户时遇到一些错误。

For this example I will use one problem user: RIÇK 在此示例中,我将使用一个有问题的用户:RIÇK

As you can see RICK != RIÇK. 如您所见,RICK!=RIÇK。 So when I do the data scrub from the site I get the correct value which I push onto an array for later use. 因此,当我从站点进行数据清理时,我得到了正确的值,并将其推送到数组上以备后用。

Once I need the player names I pull from the array as follows: (player_names is the array) 一旦我需要播放器名称,就可以从数组中提取,如下所示:(player_names是数组)

@temp_player = User.find_by_username(player_names[i].to_s) 

The problem is the users with any special characters are not being pulled. 问题是没有拉出具有任何特殊字符的用户。 Should I not be using find_by? 我不应该使用find_by吗? Is to_s changing my original values? to_s是否要更改我的原始值? I am really quite lost on what to do and would greatly appreciate any help / advice. 我真的对如何做非常迷失,非常感谢任何帮助/建议。

Thanks in advance, 提前致谢,

Dan

I would like to thank Brian Kung for the link to the following: joelonsoftware.com/articles/Unicode.html It does a great job giving the bare minimum a programmer truly needs to understand. 我要感谢Brian Kung提供的指向以下内容的链接: joelonsoftware.com/articles/Unicode.html所做的非常出色,它为程序员提供了真正需要了解的最低限度的知识。

For my particular issue I had used a HTML scraper to get the contents but which kept HTML entries throughout. 对于我的特定问题,我曾使用HTML抓取工具来获取内容,但始终保留HTML条目。 When using these with my SQL lookups it was obvious that things were not being found. 将这些与我的SQL查找一起使用时,很明显没有找到任何东西。 In order to fix this I used the HTMLEntities Gem to decode the text as follows (as soon as I put the into the array originally): 为了解决这个问题,我使用了HTMLEntities Gem来对文本进行解码,如下所示(我一开始将其最初放入数组中):

requires 'RubyGems' #without this cannot include htmlentries as a gem
requires 'HTMLEntries' 

coder = HTMLEntries.new

line = '<'
player_names.push(coder.decode(line))

The Takeaway 外卖

When working with text and if running into errors I would strongly recommend tracing the strings you are working with to the origin and truly understanding what encoding is being used in each process. 在处理文本时,如果遇到错误,我强烈建议您将要使用的字符串追溯到原点,并真正了解每个过程中使用的编码。 By doing this you can easily find where things are going wrong. 通过这样做,您可以轻松找到问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM