簡體   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