Chartkick时间轴未定义方法`each_pair'用于#<数组:...>在Rails 4中

时间:2015-11-17 19:37:00

标签: ruby-on-rails-4 chartkick

我在Rails 4中遇到了Chartkick时间轴图表的问题。 页面加载时抛出此错误:

“#< Array:...>”的未定义方法`each_pair'

在Chartkick代码中的

如下所示;

 def chartkick_deep_merge(hash_a, hash_b)
  hash_a = hash_a.dup
  hash_b.each_pair do |k, v|
    tv = hash_a[k]
    hash_a[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
  end
  hash_a
end

我尝试使用to_json,as_json,@ events.each do ...,@ events.map ...尝试进行各种代码迭代     时间轴(“chart-1”,“phase_masters_phase_time_path”) 这指向     render json:Events.select(:id,:event_types,:starttime,:endtime).where(project_masters_id:params [:proj_id],phase_masters_id:params [:id])

变量似乎正确设置,如hash_b

中所示
hash_a  
{:width=>"400px", :height=>"400px", :colors=>["#B9D3EE", "#9FB6CD", "#A2CD5A", "#6E8B3D", "#FFB90F", "#E3A869"]}

hash_b  
[[#<Events id: 1, event_types: "Estimating", starttime: "2015-10-05 11:15:00", endtime: "2015-10-13 21:15:00">], [#<Events id: 2, event_types: "Roll Forming", starttime: "2015-09-09 11:00:01", endtime: "2015-09-16 18:29:59">], [#<Events id: 7, event_types: "Magest On Site", starttime: "2015-09-25 11:00:01", endtime: "2015-09-25 18:29:59">]]

view:
 <div id = "chart-1"> </div>
   <%= 
    timeline("chart-1", @events.map { |evnt| [evnt] } )
   %>


controller:
@events = Events.select(:id, :event_types, :starttime, :endtime).where(project_masters_id: params[:proj_id], phase_masters_id: params[:id])
        respond_to do |format|
            format.html
            format.json {render json: @events}
        end

任何有助于此运行的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

好像你将Events数组传递给方法timeline,该方法需要一个三元组数组(类型为数组)。

我不确定我是否正确阅读您的代码,但似乎timeline的调用可能会像

一样

timeline("chart-1", @events.map { |evnt| [evnt.types, evnt.starttime, evnt.endtime] })