简体   繁体   中英

Using the Acts_as_votable gem with Angularjs

Basically I want to access Acts_as_Votable through an Angular template. I have a list of cars that people can vote on. However, I can't display the votes when the list is rendered in Angular.

I'm guessing I need to pass some sort of association for :votes when the json gets rendered in the controller, like with :marque. This is where I've got to so far. Can't for the life of me work it out and resorted to actually asking a question rather!

Cheers

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>

As soon as I posted the question, I worked out the answer. Typical.

You add votes_for with the json

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

And tally it up in Angular

<li>Votes: {{car.votes_for.length}} </li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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