无法阅读财产“所有'未定义的Restangular

时间:2016-01-26 04:38:00

标签: ruby-on-rails angularjs

我正在开发一个集成了Rails和AngularJS的应用程序,并且我试图对所有REST功能使用restangular,问题是当我尝试发布数据来创建记录时,我得到了错误Can not read property 'all' of undefined at Scope.$scope.addPost

我得到错误的角度控制器就是这个。

poll.controller.js

angular.module('myapp')

.controller('CreatePollCtrl', ['$scope', 'Restangular', function($scope, Restangular) {
  $scope.encuesta = {title: "Encuesta docente"};
  $scope.addPost = function($scope, Restangular) {
    Restangular.all('polls').post($scope.encuesta).then(function() {
    });
  };
}]);

我尝试的是向我的Rails控制器发送一个名为 posts_controller.rb的帖子请求

class PollsController < ApplicationController
  before_action :set_poll, only: [:show, :edit, :update, :destroy]

  # GET /polls
  # GET /polls.json
  def index
    @polls = Poll.all
  end

  # GET /polls/1
  # GET /polls/1.json
  def show
  end

  # GET /polls/new
  def new
    @poll = Poll.new
  end

  # GET /polls/1/edit
  def edit
  end

  # POST /polls
  # POST /polls.json
  def create
    @poll = Poll.new(poll_params)

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

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

  # DELETE /polls/1
  # DELETE /polls/1.json
  def destroy
    @poll.destroy
    respond_to do |format|
      format.html { redirect_to polls_url, notice: 'Poll was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def poll_params
      params[:poll]
    end
end

只是为了告诉你路线是正确的:

rake routes

还有什么我应该做的吗?

1 个答案:

答案 0 :(得分:0)

嗯,这完全没问题,我只需删除$scopeRestangular作为$scope.encuesta下面函数的参数,它完美地运行了D;