简体   繁体   English

Rails控制器添加引号

[英]Rails controller adding quotation marks

I have an Openstruct object: 我有一个Openstruct对象:

[#<OpenStruct date="20150602", pageviews="46">, #<OpenStruct date="20150603", pageviews="44">]

In my view, I need to render in a format to be used for charting, so something like [["20150602", 46], ["20150603", 44]] 我认为,我需要呈现一种用于图表绘制的格式,因此类似[["20150602", 46], ["20150603", 44]]

In my view, I can achieve this with something like: 我认为,可以通过以下方式实现此目的:

-@visits.map do |v|
  ="[#{v.date}, #{v.pageviews}],"

And it works just fine. 而且效果很好。 But I want it in the controller, so I don't need all that code in the view and I can just insert it into the data. 但是我想要在控制器中使用它,因此我不需要视图中的所有代码,而只需将其插入数据中即可。

So I try this line in the controller: 因此,我在控制器中尝试以下行:

@visits_mapped = @visits.map {|v| "[#{v.date}, #{v.pageviews}],"}

However, now a whole new bunch of quotation marks have been added in when I print @visits_mapped in the view, for no apparent reason: 但是,当我在视图中打印@visits_mapped时,现在已经添加了一堆新的引号,没有明显的原因:

["[20150602, 46],", "[20150603, 44],"]

Why would the behaviour change between the view and the controller like this? 为什么这样在视图和控制器之间改变行为? Forgive me if this is a stupid question... 如果这是一个愚蠢的问题,请原谅我。

The behavior has not changed. 行为没有改变。 In the view you are just not seeing the output of map. 在视图中,您只是看不到map的输出。 Map iterates the members of an array and produces a new array holding the results of the iterations. Map会迭代数组的成员,并生成一个保存迭代结果的新数组。

In the view you are getting what you 'expected' because you are discarding the final result of map and instead are showing the results of each iteration. 在视图中,您得到的是“预期”,因为您将放弃map的最终结果,而是显示每次迭代的结果。 Since they are shown with nothing in between them, the output in the view 'looks' right (but should be missing the outer array [] and should have an extra , after the last element). 由于它们之间没有任何显示,因此视图中的输出“看起来”正确(但应缺少外部数组[],并且在最后一个元素之后应有一个多余的)。 You can get the same result in the view if you change the map to each (which should be more efficient as it wont generate the resulting array). 如果将映射更改为每个映射,则可以在视图中获得相同的结果(这应该更有效,因为它不会生成结果数组)。 So in the view: 所以在视图中:

    -@visits.each do |v|
      ="[#{v.date}, #{v.pageviews}],"

Or in the controller: 或在控制器中:

    @visits_mapped = @visits.map{ |v| [ v.date, v.pageviews ] }

I'm 95% sure you're just making a really roundabout attempt at generating JSON. 我有95%的肯定您只是在进行JSON的回旋尝试。 To generate JSON you should always use to_json . 要生成JSON,您应该始终使用to_json

visits = [ OpenStruct.new(date: "20150602", pageviews: "46"),
           OpenStruct.new(date: "20150603", pageviews: "44") ]

# Convert the array of OpenStructs to an array of two-element arrays,
# converting `pageviews` to an integer while we're at it
@visits_mapped = visits.map {|visit| [ visit.date, visit.pageviews.to_i ] }

puts @visits_mapped.to_json
# => [["20150602",46],["20150603",44]]

Your comments above suggest that you want the output to be in a data-visits attribute, which you can conveniently do like this in Haml: 上面的评论建议您希望输出位于data-visits属性中,您可以在Haml中方便地执行以下操作:

#chart_div{ data: { visits: @visits_mapped.to_json } }

That template will evaluate to the following HTML: 该模板将评估为以下HTML:

<div data-visits='[["20150602",46],["20150603",44]]' id='chart_div'></div>

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

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