ruby-on-rails – Rails 4.1 ActionController ::使用AngularJS
发布时间:2020-12-17 04:15:01 所属栏目:百科 来源:网络整理
导读:尝试在rails控制器中使用respond_to和respond_with时,继续获取ActionController:UnknownFormat错误.控制器中以下行出错. respond_with @surveys 在/ app / assets / controllers / surveys_controller中 respond_to :jsondef index @surveys = Survey.all r
|
尝试在rails控制器中使用respond_to和respond_with时,继续获取ActionController:UnknownFormat错误.控制器中以下行出错.
respond_with @surveys 在/ app / assets / controllers / surveys_controller中 respond_to :json def index @surveys = Survey.all respond_with @surveys end 在/config/routes.rb中 WebInsight::Application.routes.draw do
scope :api do
resources :surveys,defaults: {format: 'json'}
end
resources :surveys
end
在/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>
在/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();
}]);
解决方法
它有接缝
respond_to :json 路由将json定义为默认值时有效 scope :api,defaults: {format: :json} do
resources :resources
end
不允许控制器中的html只是将以下内容添加到方法中 def new
@resource = Resource.new
respond_with(@resource) do |format|
format.html { render nothing: true,status: :unauthorized }
format.json { render :json => @resource.as_json }
end
end
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读
