0

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

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

干杯

汽车控制器.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>
4

1 回答 1

0

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

你用 json 添加 votes_for

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>
于 2016-08-17T14:03:12.043 回答