简体   繁体   English

在Rails表单参数名称中编码UTF-8

[英]Encoding UTF-8 in Rails form parameter name

I'm having a problem with my params. 我的参数有问题。 I'm receiving the following parameters: 我收到以下参数:

{"utf8"=>"✓", "authenticity_token"=>"...=", "Portugu\xC3\xAAs"=>{"title"=>"313" } }

In my controller I need to use the key => "Portugu\\xC3\\xAAs", but first I need it to be in the right form (that is -> Português) and I don't know how can I do that. 在我的控制器中,我需要使用键=>“ Portugu \\ xC3 \\ xAAs”,但是首先我需要它采用正确的形式(即->Português),我不知道该怎么做。

EDIT: 编辑:

Workflow 1. The user saves a language 2. I use that language in a form to save information, like this: 工作流程1.用户保存一种语言2.我以某种形式使用该语言来保存信息,如下所示:

Português[title]

3 . 3。 Because the user can have multiple locales in that form (all the locales saved in step 1) 因为用户可以使用该格式的多个语言环境(所有语言环境在步骤1中保存)

locales.each do |locale|
 ...
 :value => params[locale.key][:title]

The problem is that locale.key ('Português') doesn't match with "Portugu\\xC3\\xAAs" so it crashes with nil 问题是locale.key('Português')与“ Portugu \\ xC3 \\ xAAs”不匹配,因此崩溃为nil

Can you help me with this? 你能帮我吗? Thank you 谢谢

I've tried this, and the result is good: 我已经尝试过了,结果很好:

    <% p = {}
       p["Português"] = {}
       p["Português"][:title] = "Title in Portugês" %>
    <p><%= p["Portugu\xC3\xAAs"][:title] %>

And I get 我得到

<p>Title in Portugês</p>

I don't see the problem. 我没看到问题。

The solution that worked for me was iterating the received params and with the help of URI.escape compare the string, if matches enc_locale is set and used in the value. 对我有用的解决方案是迭代接收到的参数,并在URI.escape的帮助下比较字符串(如果设置了匹配的enc_locale并在值中使用)。 Thanks to everyone that helped! 谢谢大家的帮助!

    enc_locale = ""
    params.each do |param|
      if URI.escape(param[0]) == URI.escape(locale.key)
        enc_locale = param[0]
      end
    end

   ...
   :value => params[enc_locale][:title]

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

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