문제

I'm using morris.js to display charts in my application. In my controller I have the code:

controller

@users_count = User.group('DATE(created_at)').count.map do |d, c| {date: d, count: c} end

From this I get:

[{:date=>'2013-04-01', :count=>1}, {:date=>'2013-04-02', :count=>1}]

When I try to pass this to morris.js as a data option it doesn't work. I need to convert this to:

[{date: '2013-04-01', count: 1}, {date: '2013-04-02', count: 1}]

Then it works.

How can I do that?

도움이 되었습니까?

해결책

This has nothing to do with old vs new hash syntax. You cannot convert output to the new syntax. It will always use the old syntax. What morris expects is probably a JSON string. You can get it by using .to_json.

Use to_json on your result and pass that to morris.

@users_count = User.group('DATE(created_at)').count.map { |d, c| {date: d, count: c} }.to_json
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top