【问题标题】:Using the Acts_as_votable gem with Angularjs将 Acts_as_votable gem 与 A​​ngularjs 一起使用
【发布时间】:2016-08-17 13:26:41
【问题描述】:

基本上我想通过 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>

【问题讨论】:

    标签: javascript ruby-on-rails angularjs json acts-as-votable


    【解决方案1】:

    我一发布问题,就找到了答案。典型的。

    你用 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>
    

    【讨论】:

      猜你喜欢
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多