Tom*_*ond 2 ruby ruby-on-rails
我正在玩Netflix的Workflowable宝石.现在我正在制作一个自定义动作,用户可以选择.
我最终{"id":1,"value":"High"}退出了@options[:priority][:value]
我想要做的是获取id值为1.任何想法如何把它拉出来?我试过@options[:priority][:value][:id]但似乎是因为错误.
这是动作的样子/我如何记录值:
class Workflowable::Actions::UpdateStatusAction < Workflowable::Actions::Action
include ERB::Util
include Rails.application.routes.url_helpers
NAME="Update Status Action"
OPTIONS = {
:priority => {
:description=>"Enter priority to set result to",
:required=>true,
:type=>:choice,
:choices=>[{id: 1, value: "High"} ]
}
}
def run
Rails.logger.debug @options[:priority][:value]
end
end
Run Code Online (Sandbox Code Playgroud)
这是错误:
Error (3a7b2168-6f24-4837-9221-376b98e6e887): TypeError in ResultsController#flag
no implicit conversion of Symbol into Integer
Run Code Online (Sandbox Code Playgroud)
这是@options[:priority]看起来像:
{"description"=>"Enter priority to set result to", "required"=>true, "type"=>:choice, "choices"=>[{"id"=>1, "value"=>"High"}], "value"=>"{\"id\":1,\"value\":\"High\"}", "user_specified"=>true}
Run Code Online (Sandbox Code Playgroud)
@options[:priority]["value"]看起来是一个强大的包含json,而不是哈希.这就是使用时出错的原因[:id](此方法不接受符号)以及为什么["id"]返回字符串"id".
您需要先解析它,例如JSON.parse,在此时您将拥有一个您应该能够正常访问的哈希值.默认情况下,键将是字符串,因此您需要
JSON.parse(值)[ "ID"]