繁体   English   中英

在Angularjs中使用Acts_as_votable gem

[英]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>

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

您添加带有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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM