[英]Using the Acts_as_votable gem with Angularjs
Basically I want to access Acts_as_Votable through an Angular template. 基本上,我想通过Angular模板访问Acts_as_Votable。 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. 但是,当列表以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. 我猜想当控制器中渲染json时,我需要通过:votes的某种关联,例如: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 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 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 您添加带有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
And tally it up in Angular 并在Angular中进行汇总
<li>Votes: {{car.votes_for.length}} </li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.