Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Keep getting ActionController:UnknownFormat error when trying to use respond_to and respond_with in rails controller. Error on the following line in controller.

respond_with @surveys 

In /app/assets/controllers/surveys_controller

respond_to :json

def index
  @surveys = Survey.all
  respond_with @surveys
end

In /config/routes.rb

WebInsight::Application.routes.draw do

   scope :api do
      resources :surveys, defaults: {format: 'json'}
   end

   resources :surveys
end

In /app/assets/views/surveys/index.html.erb

<h1>Your Surveys</h1>

<form>
  <input type="text" ng-model='newSurvey' placeholder="Enter a survey">
  <input type="submit" value="Add">
</form>

<div ng-controller="SurveysCtrl">
    <ul>
        <li ng-repeat="survey in surveys">
          {{ survey.theme_id }}, {{ survey.name }}, {{ survey.description }}
        </li>
    </ul>
</div>

In /app/assets/javascripts/angular/controllers/surveys_ctrl.js

app.controller('SurveysCtrl', ['$scope', '$resource', function($scope, $resource) {
   var Surveys = $resource('/api/surveys');
   $scope.surveys = Surveys.query();
}]);
share|improve this question

1 Answer 1

Solved it with the following amendment:

In /app/assets/controllers/surveys_controller, use

respond_to :json, :html

instead of just

respond_to :json
share|improve this answer
    
A word of caution here: If you put respond_to :json, :html, it means rails will continue to serve HTML output using rails, not angular. If you want AngularJS to be serving all HTML content as the front-end, while rails only handles back-end (as should be the case for 1 page apps), you should not enable respond_to :html. Instead, read up on AngularJS routing. [scotch.io/tutorials/javascript/… –  CS Koh Jul 4 at 11:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.