使用 Acts_as_votable gem 和 Angularjs

Using the Acts_as_votable gem with Angularjs

基本上我想通过 Angular 模板访问 Acts_as_Votable。我有一份人们可以投票的汽车清单。但是,在 Angular 中呈现列表时,我无法显示投票。

我猜想当 json 在控制器中呈现时,我需要为 :votes 传递某种关联,例如 :marque。到目前为止,这是我必须要做的。不能为我的生活解决这个问题,而是求助于实际问一个问题!

干杯

cars_controller.rb

def index
  @cars = Car.all
  respond_to do |format|
    format.html {}
    format.json {render json: @cars, :include => :marque}
  end
end

index.html.erb

<div ng-repeat="car in cars | rangeFilter:slide | filter:name">
  <div class="col-sm-6 col-md-4">
     <ul class="thumbnail">
        <li>{{car.marque.name}}</li>
        <li>{{car.name}}</li>
        <li>{{car.power}}</li>
        <li>{{car.rotor}}</li>
        <li>Votes: {{car.votes_for.size}} </li>
        <li><%= link_to 'Show', URI::unescape(turbine_path('{{car.id}}'))%></li>
        <li><%= link_to "up", URI::unescape(like_turbine_path('{{car.id}}')), method: :put, class: "btn btn-default btn-sm" %></li>
      </ul>
    </div>
 </div>

我一发布问题,就得出了答案。典型的。

您添加 votes_for 与 json

def index
  @cars = Car.all
  respond_to do |format|
    format.html {}
    format.json {render json: @cars, :include => [:marque, :votes_for] }
  end
end  

并在 Angular

中进行统计
<li>Votes: {{car.votes_for.length}} </li>