“属性应该是一个哈希,但是是一个字符串” [英] "Attribute was supposed to be a Hash, but was a String"

查看:127
本文介绍了“属性应该是一个哈希,但是是一个字符串”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在保存我的Rails 3应用程序中的散列时遇到了问题。我可以在使用控制台时保存它 - 当我通过表单提交散列时,它不起作用。

I'm having problems saving a hash in my Rails 3 application. I can save it OK when using the console - it just doesn't work when I submit the hash through a form.


This SO question addresses it but the solution's not working for me. Moreover, if I use:

 serialize :bulk_action, Hash

我得到错误:

I get the error:

 Attribute was supposed to be a Hash, but was a String

通过表单保存的哈希值如下所示:

The hash, when saved through the form looks like this:

 "{\"location\"=>{\"commands\"=>{\"custom_command_one\"=>\"true\", \"custom_command_two\"=>\"true\"}}}"

而通过控制台:

Whereas, through the console:

{"location"=>{"commands"=>{"custom_command_one"=>"true", "custom_command_two"=>"true"}}}

我的数据库字段是一个文本字段。什么是通过表单保存散列的最好方法?

My database field is a text field. What's the best way to go about saving a hash through a form?

- 编辑 -

我我发现我可以解决这个问题,如果我使用符号而不是字符串作为名称,但它仍然输出一个长字符串,而不是哈希。

I've found I can sort of get around the problem if I use symbols not strings for the names but it still outputs one long string, not the hash.

推荐答案

你能否在textarea中切换到JSON,以便解析它并不那么危险。因为你需要做的是评估控制器或模型中的相应参数条目,以使用户可以根据自己的需要为应用程序运行的用户执行任何操作。使用JSON,您可以在设置模型属性之前使用 JSON.parse

Can you switch to JSON within the textarea so that parsing it isn't so dangerous. Because what you would have to do is eval the respective params entry in a controller or the model which enables users to do whatever they want with the user under which your app is running. With JSON, you could just use JSON.parse before setting the model attribute.

这篇关于“属性应该是一个哈希,但是是一个字符串”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆