CS *_*Koh 7 ruby-on-rails angularjs ruby-on-rails-4
尝试在rails控制器中使用respond_to和respond_with时,继续获取ActionController:UnknownFormat错误.控制器中以下行出错.
respond_with @surveys
Run Code Online (Sandbox Code Playgroud)
在/ app/assets/controllers/surveys_controller中
respond_to :json
def index
@surveys = Survey.all
respond_with @surveys
end
Run Code Online (Sandbox Code Playgroud)
在/config/routes.rb中
WebInsight::Application.routes.draw do
scope :api do
resources :surveys, defaults: {format: 'json'}
end
resources :surveys
end
Run Code Online (Sandbox Code Playgroud)
在/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>
Run Code Online (Sandbox Code Playgroud)
在/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();
}]);
Run Code Online (Sandbox Code Playgroud)
通过以下修改解决了这个问题:
在 /app/assets/controllers/surveys_controller 中,使用
respond_to :json, :html
Run Code Online (Sandbox Code Playgroud)
而不是仅仅
respond_to :json
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10128 次 |
最近记录: |