我正在开发一个rails 4.1.8API,并在客户端使用angular js,但使用的是不同的工作区。我使用grunt connect- proxy代理所有请求到我的rails API,但是Rails总是返回给我No route matches[METHOD]"ROUTE",但是尝试使用高级Rest客户端工作得很好,我不知道我出了什么问题。域不是问题所在,它们正在工作
routes.rb
namespace :api, path: '/', constraints: {subdomain: 'api'}, defaults: { format: :json } do
namespace :v1 do
with_options except: [:edit, :new] do |except|
except.resources :users do
collection do
post 'login'
end
end
except.resources :products
except.resources :locations
end
end
end我的angular代码如下所示
angular.module('myApp')
.controller('HomeCtrl', function ($scope, $resource) {
var User = $resource('/v1/users',{},{});
User.get().$promise.then(function (data) {
console.log(data);
});
});这是我的Grunt代理配置
grunt.initConfig({
connect: {
options: {
port: 9000,
hostname: 'dev.myapp.co',
livereload: 35729
},
proxies: [
{
context: '/v1',
host: 'api.myapp.co',
port: 3000
}
],
livereload: {
options: {
open: true,
middleware: function (connect) {
return [
require('grunt-connect-proxy/lib/utils').proxyRequest,
connect.static('.tmp'),
connect().use(
'/bower_components',
connect.static('./bower_components')
),
connect.static(appConfig.app)
];
}
}
},
...
});
grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'connect:dist:keepalive']);
}
grunt.task.run([
'clean:server',
'wiredep',
'concurrent:server',
'autoprefixer',
'configureProxies',
'connect:livereload',
'watch'
]);
});
grunt.loadNpmTasks('grunt-connect-proxy');发布于 2014-12-15 15:36:31
我怀疑你可能给你的Rails控制器起错了名字。您是否介意发布user_controller、product_controller或location_controller?谢谢!
另外,当您尝试通过localhost:3000/v1/users直接访问api时,会发生什么情况?您是否收到相同的No route matches[METHOD]"ROUTE"错误?
https://stackoverflow.com/questions/27338660
复制相似问题