将哈希作为值传递到hidden_field_tag
我试图通过如下形式传递参数中的一些过滤器:
hidden_field_tag "filters", params[:filters]
由于某种原因,参数在下一页中发生了更改。例如,如果 params[:filters] 曾经是...
"filters"=>{"name_like_any"=>["apple"]} [1]
...它会改变...
"filters"=>"{\"name_like_any\"=>[\"apple\"]}" [2]
注意 [2] 中的额外引号和反斜杠与[1]相比。
有什么想法吗?我试图将其与 searchlogic 一起使用进行一些过滤,但当我更改表单中的更改对象时,我需要它保留下来。我不想将其存储在会话中。
I am trying to pass some filters in my params through a form like so:
hidden_field_tag "filters", params[:filters]
For some reason the params get changed in the next page. For example, if params[:filters] used to be...
"filters"=>{"name_like_any"=>["apple"]} [1]
...it gets changed to...
"filters"=>"{\"name_like_any\"=>[\"apple\"]}" [2]
note the extra quotations and backslashes in [2] when compared to [1].
Any ideas? I'm attempting to use this with searchlogic for some filtering, but I need it to persist when I change change objects in forms. I would prefer not to have to store it in session.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我的解决方案只是用键值对重新创建每个参数:
My solution was just to re-create each of param with key-value pair:
您实际上想要/需要使用隐藏字段“序列化”哈希。
将其添加到您的 ApplicationHelper 中:
然后,在视图中:
即使您的过滤器中有多级哈希/数组,这也应该可以解决问题。
采取的解决方案http://marklunds.com/articles/one/314
You actually want/need to 'serialize' a hash using hidden fields.
Add this to your ApplicationHelper :
Then, in view:
This should do the trick, even if you have a multilevel hash/array in your filters.
Solution taken http://marklunds.com/articles/one/314
我刚刚编写了一个名为 HashToHiddenFields 的 gem 来执行此操作。
gem 的核心是这段代码:
I just wrote a gem to do this called HashToHiddenFields.
The core of the gem is this code:
以下是我如何设法通过我的视图传递参数值 - 即从视图 A 到视图 B 再到控制器:
在我能找到的任何地方都没有记录的关键转换是 {... :id =>;视图 A 中的 object.id} 作为 <%... :id => 传递到视图 B @object %>,然后视图 B 通过hidden_field_tag 构造将其作为 (:param_name, params[:id]) 传递到控制器。
我没有在任何地方看到这个记录,但在仔细阅读了包括这篇文章在内的多个网站上的几篇文章(其语法提供了关键灵感)后,解决方案最终得到了解决。我已经看到了与安全相关的隐藏字段的警告,但鉴于我当前的设计,没有找到其他方法来做到这一点。
Here's how I managed to pass a parameter value through my view - that is, from View A through View B and on to the controller:
The key transition that wasn't documented anywhere I could find is where {... :id => object.id} from View A is passed on to View B as <%... :id => @object %>, which View B then passes on to the controller as (:param_name, params[:id]) through the hidden_field_tag construct.
I didn't see this documented anywhere but after perusing several posts across several sites including this post (whose syntax provided the key inspiration), the solution finally gelled. I've seen the caveats on hidden fields pertaining to security but have found no other way to do this given my current design, such as it is.
这是因为当您使用hidden_field_tag转换为HTML时,会添加反引号。当你收到它时,它就像一个字符串而不是哈希值。
Hash 类型不能存在于 HTML 中。你只有字符串。因此,如果您想传递哈希值(我不推荐),则需要在收到哈希值时对其进行评估。但这可能对您的应用程序来说是一个很大的安全问题。
it's because when you convert in HTML with your hidden_field_tag, the backquote is add. After when you received it like a string not a Hash.
The Hash type can't exist in HTML. You have only string. So if you want pass your hash (not recommend by me), you need eval it when you received it. But can be a big security issue on your application.
作为对 Vlad 答案的警告,我必须使用 raw:
让它在 Rails 3.1.1 中工作。本质上,正在输出的文本被转义,例如“<”变成“<”。
As a caveat to Vlad's answer, I had to use raw:
to get it to work in Rails 3.1.1. Essentially, the text being output was being escaped, eg., "<" becoming "<".
假设哈希是字符串、符号、数字和数组,您可以调用 eval 将哈希的 params 字符串从hidden_fields 形式转换回控制器中的哈希。然后,添加的引号的反斜杠转义字符不再是问题:
感谢以下文章帮助为我的案例确定了这个简单的解决方案:
如何将 String 对象转换为 Hash 对象?
请记住,参数的内容应使用 .require 进行清理和.许可。
Assuming the hash is strings, symbols, numbers, and arrays, you can call eval to convert the params string of the hash from the hidden_fields form back into a hash in the controller. Then the backslash escape characters for the quotes added are no longer an issue:
Credit to the following article for helping identify this simple solution for my case:
How do I convert a String object into a Hash object?
Keep in mind the contents of the params should be cleaned with .require and .permit.