1

I've create a mini poll game and I want to show the user's votes in pie chart. I used Chartkick to do that, my code here:

<div class="form-group">
    <%= content_tag(:label) do %>
        <% unless current_user.voted_for?(@poll) %>
            <%= radio_button_tag 'vote_option[id]', option.id %>
        <% end %>
        <%= option.title %>
    <% end %>
    <%= visualize_votes_for option %>
    <%= pie_chart @poll.group(:title).count('votes') %>
</div>

I expected to display option.title and option.votes correspondingly. Here is a vote_option.rb model:

class VoteOption < ActiveRecord::Base
  belongs_to :poll
  has_many :votes, dependent: :destroy
  has_many :users, through: :votes

  validates :title, presence: true
end

And here is the error that I got:

ActionView::Template::Error (undefined method `group' for #<Poll:0x007f0a1c2610b0>)

Any idea for this issue?

4

1 回答 1

2

已解决 经过数小时的搜索,我发现了我的错误:我们不能在模型的实例上调用 .group,我们必须在模型类上调用它,这就是为什么我得到这个错误的原因。

于 2015-03-28T03:40:00.810 回答