Controllers, views, helpers, and javascript

15 views
Skip to first unread message

Jussi Hirvi

unread,
May 19, 2018, 3:35:19 PM5/19/18
to rubyonra...@googlegroups.com
I am doing charts with https://www.chartkick.com/ and basically my
installation already works. However, I can access the chart js libraries
only from views. In a view I can say pie_chart (with parameters), and I
will get a nice pie chart.

But I would like to compose the page html in controllers so I would not
need to enclose every damn line in <% %>. But for controllers, the graph
suite's command pie_chart is a "command not found".

Then I tried a helper, and calling the helper's method:

def piechart_test
pie_chart({"a" => 4, "b" => 5, "c" => 7})
end

in a controller. Partial success. I get the following output:

<div id="chart-1" style="height: 400px;width: 100%;text-align:
center;color: #999;line-height: 400px;font-size: 14px;font-family:
'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica,
sans-serif;">Loading...</div>
new Chartkick.PieChart("chart-1", {"a":4,"b":5,"c":7},
{"colors":["pink","#999"]});

It says "Loading...", but it never loads. Whereas if I call the same
helper method from the view, the graph loads ok.

I could now move my whole html composition from the controller to a
helper, and then call that from the view. A downside is that helpers are
not namespaced, so it would feel a less clean solution.

Tips would be appreciated! BTW, Rails 5.x.

- Jussi
Reply all
Reply to author
Forward
0 new messages