2016-03-10 50 views
1

我使用的是Angular作爲一個組件,它具有引用作爲Rails後端的資源。 Angular被加載到Dom中,但是查詢方法不會從Rails後端返回任何結果。我收到一個錯誤,說Quotes.query不是一個函數。如果你知道我可能做錯了什麼,請告訴我。Angular沒有從Rails數據庫查詢資源

行情控制器

class QuotesController < ApplicationController 
    before_action :set_quote, only: [:show, :edit, :update, :destroy] 
    respond_to :html, :json 
    # GET /quotes 
    # GET /quotes.json 
    def index 
    @quotes = Quote.all 
    respond_with(@quotes) do |format| 
     format.json { render :json => @quote.as_json } 
     format.html 
    end 
    end 

    # GET /quotes/1 
    # GET /quotes/1.json 
    def show #shows some material 
    respond_with(@quote.as_json) 
    # @quote = Quote.find(params[:id]) 
    # respond_to do |format| 
    # format.html 
     # format.pdf do 
     # pdf = QuotePdf.new(@quote) 
     # send_data pdf.render, filename: "quote_#{@quote.id}", 
     #       type: "application/pdf", 
     #       disposition: "inline" 

     # end 
    # end 
    end 
    # GET /quotes/new 
    def new 
    @quote = Quote.new 
    end 

    # GET /quotes/1/edit 
    def edit 
    end 

    # POST /quotes 
    # POST /quotes.json 
    def create 
    @quote = Quote.new(quote_params) 

    respond_to do |format| 
     if @quote.save 
     format.html { redirect_to root_path, notice: 'Quote was successfully created.' } 
     format.json { render :show, status: :created, location: @quote } 
     else 
     format.html { render :new } 
     format.json { render json: @quote.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /quotes/1 
    # PATCH/PUT /quotes/1.json 
    def update 
    respond_to do |format| 
     if @quote.update(quote_params) 
     format.html { redirect_to @quote, notice: 'Quote was successfully updated.' } 
     format.json { render :show, status: :ok, location: @quote } 
     else 
     format.html { render :edit } 
     format.json { render json: @quote.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /quotes/1 
    # DELETE /quotes/1.json 
    def destroy 
    @quote.destroy 
    render json: { status: :ok} 
    # respond_to do |format| 
    # format.html { redirect_to quotes_url, notice: 'Quote was successfully destroyed.' } 
    # format.json { head :no_content } 
    # end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_quote 
     @quote = Quote.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def quote_params 
     params.require(:quote).permit(:name, :email, :address, :city, :state, :region, :question1, :question2, :question3, :question4, :question5, :question6, :question7, :question8, :question9, :question10, :question11, :question12, :question13, :question14, :image) 
    end 
end 

AngularJS

var app = angular.module("QuotesApp", ['ngRoute', 'ngResource']); 

app.factory('Quotes', ['$resource',function($resource){ 
return $resource('/quotes.json', {},{ 
query: { method: 'GET', isArray: true }, 
create: { method: 'POST' } 
}) 
}]); 

app.factory('Quote', ['$resource', function($resource){ 
return $resource('/quotes/:id.json', {}, { 
show: { method: 'GET' }, 
update: { method: 'PUT', params: {id: '@id'} }, 
delete: { method: 'DELETE', params: {id: '@id'} } 
}); 
}]); 

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { 
     $routeProvider.when('/quotes',{ 
      templateUrl: '/views/quotes/index.html', 
      controller: 'QuotesCtrl' 
     }); 
     $routeProvider.when('/quotes/:id/show', { 
      templateUrl: '/views/quotes/show.html.erb', 
      controller: "QuotesCtrl" 
     }); 
     $routeProvider.otherwise({ 
      redirectTo: '/quotes' 
     }); 
    } 
]); 

app.controller("QuotesCtrl", ['$scope', '$resource', '$http', 'Quotes', 'Quote', '$location', function($scope, $http, Quotes, Quote, $location) { 
     $scope.quotes = Quotes.query(); 
    } 
]); 

行情指數

<div ng-app="QuotesApp"> 
<div class="quotes col-xs-10 col-md-8" ng-controller="QuotesCtrl"> 
    <li ng-repeat="quote in quotes"> 
    {{quotes.name}} 
    </li> 
</div> 

回答

1

直噴直列排列註釋

app.controller("QuotesCtrl", ['$scope', '$resource', '$http', 'Quotes', 'Quote', '$location', 
    function($scope, $resource, $http, Quotes, Quote, $location) { 

錯過$resource OR你可以刪除$resource,這似乎是多餘的

app.controller("QuotesCtrl", ['$scope', '$http', 'Quotes', 'Quote', '$location', 
    function($scope, $http, Quotes, Quote, $location) { 
+0

潘卡你生命的救星。對此,我真的非常感激。我可能永遠不會發現這一點。你解決了我的問題。 –

+0

@Merlin傑克遜發生,很高興知道..謝謝:-) –