gpt4 book ai didi

ruby-on-rails - Rails 4.1 ActionController::UnknownFormat 错误响应使用 AngularJS

转载 作者:行者123 更新时间:2023-12-03 16:15:41 26 4
gpt4 key购买 nike

尝试在 rails Controller 中使用 respond_to 和 respond_with 时,不断收到 ActionController:UnknownFormat 错误。 Controller 中的以下行出错。

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

不允许 Controller 中的 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

关于ruby-on-rails - Rails 4.1 ActionController::UnknownFormat 错误响应使用 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24105983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com