Ruby on rails 角度ui路由器模板未加载,Rails后端
我跟在后面,我遇到了一个问题,似乎最有可能和角度有关。在我到达角度布线部分之前,一切正常。简单地说,Ruby on rails 角度ui路由器模板未加载,Rails后端,ruby-on-rails,angularjs,angular-ui-router,Ruby On Rails,Angularjs,Angular Ui Router,我跟在后面,我遇到了一个问题,似乎最有可能和角度有关。在我到达角度布线部分之前,一切正常。简单地说,标记中的内联模板不会加载到元素中。我最初认为这可能是由于将页面作为文件在本地打开,而不是从服务器加载,但即使在集成Rails之后(使用较旧版本的链轮,如中所述),同样的问题仍然存在 在运行Rails服务器时,当我将索引页作为文件或URL加载到浏览器中时,我已经检查了HTML,并且可以肯定的是,它在代码中显示的唯一内容是div和一个空的元素,这表明某些内容的添加不正确。我尝试过各种方法,包括: 使
标记中的内联模板不会加载到
元素中。我最初认为这可能是由于将页面作为文件在本地打开,而不是从服务器加载,但即使在集成Rails之后(使用较旧版本的链轮,如中所述),同样的问题仍然存在
在运行Rails服务器时,当我将索引页作为文件或URL加载到浏览器中时,我已经检查了HTML,并且可以肯定的是,它在代码中显示的唯一内容是div和一个空的
元素,这表明某些内容的添加不正确。我尝试过各种方法,包括:
- 使用最新版本的ui路由器(撰写本文时为0.2.15),而不是教程中的版本
- 使用
而不是 - 将主状态中“url”的值更改为“index.html”,包括使用文件的完整路径(file:///...)
- 将内联
模板的内容放入它们自己的文件中(当然没有
标记),并使用相对路径和完整路径指定“templateUrl”字段 - 尝试Chrome和Firefox只是为了更加确定
元素
考虑到这个问题似乎发生在Rails部分之前,它确实似乎与Angular本身有关,但我不知道发生了什么,特别是因为我一直在关注这个问题
我使用Bower来管理角度依赖关系,HTML确实显示app/assets/javascripts目录和vendor/assets/Bower\u components目录中的角度javascript文件在
部分正确加载,因此资产管道集成中的一切似乎都正常
我正在使用的版本:轨道:4.2.3
Ruby:2.2.1p85
角度:1.4.3
用户界面路由器:0.2.15
我得到的主要运动部件代码如下: app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Test App</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body ng-app="testApp">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<ui-view></ui-view>
</div>
</div>
</body>
</html>
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
respond_to :json
def angular
render 'layouts/application'
end
end
Rails.application.routes.draw do
root to: 'application#angular'
end
app/assets/javascripts/application.js
angular.module('testApp', ['ui.router', 'templates']).config(['$stateProvider', '$urlRouteProvider', function($stateProvider, $urlRouteProvider) {
$stateProvider
.state('home', {
'url': '/home',
'templateUrl': 'home/_home.html',
'controller': 'MainCtrl'
})
.state('posts', {
'url': '/posts/{id}',
'templateUrl': 'posts/_posts.html',
'controller': 'PostsCtrl'
});
$urlRouteProvider.otherwise('home');
}]);
//= require angular
//= require angular-rails-templates
//= require angular-ui-router
//= require_tree .
angular.module('testApp').controller('MainCtrl', ['$scope', 'posts', function($scope, posts) {
$scope.posts = posts.posts;
$scope.addPost = function() {
if (!$scope.title || $scope.title === "")
return;
$scope.posts.push({
'title': $scope.title,
'link': $scope.link,
'upvotes': 0,
'comments': [
{'author': 'Some Person', 'body': 'This is a comment.', 'upvotes': 0},
{'author': 'Another Person', 'body': 'This is also a comment.', 'upvotes': 0}
]
});
$scope.title = "";
$scope.link = "";
};
$scope.incrementUpvotes = function(post) {
post.upvotes++;
};
}]);
angular.module('testApp').controller('PostsCtrl', ['$scope', '$stateParams', 'posts', function($scope, $stateParams, posts) {
$scope.post = posts.posts[$stateParams.id];
$scope.addComment = function() {
if($scope.body === '')
return;
$scope.post.comments.push({
'body': $scope.body,
'author': 'user',
'upvotes': 0
});
$scope.body = '';
};
}]);
angular.module('testApp').factory('posts', ['$http', function($http) {
var o = {
'posts': []
};
o.getAll = function() {
return $http.get('/posts.json').success(function(data) {
angular.copy(data, o.posts);
});
};
return o;
}]);
app/controllers/application\u controller.rb
<!DOCTYPE html>
<html>
<head>
<title>Test App</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body ng-app="testApp">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<ui-view></ui-view>
</div>
</div>
</body>
</html>
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
respond_to :json
def angular
render 'layouts/application'
end
end
Rails.application.routes.draw do
root to: 'application#angular'
end
app/assets/javascripts/home/mainCtrl.js
angular.module('testApp', ['ui.router', 'templates']).config(['$stateProvider', '$urlRouteProvider', function($stateProvider, $urlRouteProvider) {
$stateProvider
.state('home', {
'url': '/home',
'templateUrl': 'home/_home.html',
'controller': 'MainCtrl'
})
.state('posts', {
'url': '/posts/{id}',
'templateUrl': 'posts/_posts.html',
'controller': 'PostsCtrl'
});
$urlRouteProvider.otherwise('home');
}]);
//= require angular
//= require angular-rails-templates
//= require angular-ui-router
//= require_tree .
angular.module('testApp').controller('MainCtrl', ['$scope', 'posts', function($scope, posts) {
$scope.posts = posts.posts;
$scope.addPost = function() {
if (!$scope.title || $scope.title === "")
return;
$scope.posts.push({
'title': $scope.title,
'link': $scope.link,
'upvotes': 0,
'comments': [
{'author': 'Some Person', 'body': 'This is a comment.', 'upvotes': 0},
{'author': 'Another Person', 'body': 'This is also a comment.', 'upvotes': 0}
]
});
$scope.title = "";
$scope.link = "";
};
$scope.incrementUpvotes = function(post) {
post.upvotes++;
};
}]);
angular.module('testApp').controller('PostsCtrl', ['$scope', '$stateParams', 'posts', function($scope, $stateParams, posts) {
$scope.post = posts.posts[$stateParams.id];
$scope.addComment = function() {
if($scope.body === '')
return;
$scope.post.comments.push({
'body': $scope.body,
'author': 'user',
'upvotes': 0
});
$scope.body = '';
};
}]);
angular.module('testApp').factory('posts', ['$http', function($http) {
var o = {
'posts': []
};
o.getAll = function() {
return $http.get('/posts.json').success(function(data) {
angular.copy(data, o.posts);
});
};
return o;
}]);
app/assets/javascripts/posts/postsCtrl.js
angular.module('testApp', ['ui.router', 'templates']).config(['$stateProvider', '$urlRouteProvider', function($stateProvider, $urlRouteProvider) {
$stateProvider
.state('home', {
'url': '/home',
'templateUrl': 'home/_home.html',
'controller': 'MainCtrl'
})
.state('posts', {
'url': '/posts/{id}',
'templateUrl': 'posts/_posts.html',
'controller': 'PostsCtrl'
});
$urlRouteProvider.otherwise('home');
}]);
//= require angular
//= require angular-rails-templates
//= require angular-ui-router
//= require_tree .
angular.module('testApp').controller('MainCtrl', ['$scope', 'posts', function($scope, posts) {
$scope.posts = posts.posts;
$scope.addPost = function() {
if (!$scope.title || $scope.title === "")
return;
$scope.posts.push({
'title': $scope.title,
'link': $scope.link,
'upvotes': 0,
'comments': [
{'author': 'Some Person', 'body': 'This is a comment.', 'upvotes': 0},
{'author': 'Another Person', 'body': 'This is also a comment.', 'upvotes': 0}
]
});
$scope.title = "";
$scope.link = "";
};
$scope.incrementUpvotes = function(post) {
post.upvotes++;
};
}]);
angular.module('testApp').controller('PostsCtrl', ['$scope', '$stateParams', 'posts', function($scope, $stateParams, posts) {
$scope.post = posts.posts[$stateParams.id];
$scope.addComment = function() {
if($scope.body === '')
return;
$scope.post.comments.push({
'body': $scope.body,
'author': 'user',
'upvotes': 0
});
$scope.body = '';
};
}]);
angular.module('testApp').factory('posts', ['$http', function($http) {
var o = {
'posts': []
};
o.getAll = function() {
return $http.get('/posts.json').success(function(data) {
angular.copy(data, o.posts);
});
};
return o;
}]);
app/assets/javascripts/posts/posts.js
angular.module('testApp', ['ui.router', 'templates']).config(['$stateProvider', '$urlRouteProvider', function($stateProvider, $urlRouteProvider) {
$stateProvider
.state('home', {
'url': '/home',
'templateUrl': 'home/_home.html',
'controller': 'MainCtrl'
})
.state('posts', {
'url': '/posts/{id}',
'templateUrl': 'posts/_posts.html',
'controller': 'PostsCtrl'
});
$urlRouteProvider.otherwise('home');
}]);
//= require angular
//= require angular-rails-templates
//= require angular-ui-router
//= require_tree .
angular.module('testApp').controller('MainCtrl', ['$scope', 'posts', function($scope, posts) {
$scope.posts = posts.posts;
$scope.addPost = function() {
if (!$scope.title || $scope.title === "")
return;
$scope.posts.push({
'title': $scope.title,
'link': $scope.link,
'upvotes': 0,
'comments': [
{'author': 'Some Person', 'body': 'This is a comment.', 'upvotes': 0},
{'author': 'Another Person', 'body': 'This is also a comment.', 'upvotes': 0}
]
});
$scope.title = "";
$scope.link = "";
};
$scope.incrementUpvotes = function(post) {
post.upvotes++;
};
}]);
angular.module('testApp').controller('PostsCtrl', ['$scope', '$stateParams', 'posts', function($scope, $stateParams, posts) {
$scope.post = posts.posts[$stateParams.id];
$scope.addComment = function() {
if($scope.body === '')
return;
$scope.post.comments.push({
'body': $scope.body,
'author': 'user',
'upvotes': 0
});
$scope.body = '';
};
}]);
angular.module('testApp').factory('posts', ['$http', function($http) {
var o = {
'posts': []
};
o.getAll = function() {
return $http.get('/posts.json').success(function(data) {
angular.copy(data, o.posts);
});
};
return o;
}]);
如果需要任何其他代码来帮助发现问题,请让我知道,我将提供所需的任何内容。$urlRouteProvider在我的代码中应该是$urlRouteProvider。各位,一定要仔细检查每件事,好好利用控制台 角度ui路由器似乎与新的Rails链轮不兼容。要解决此问题,请将此早期版本的链轮添加到您的文件中:
gem'链轮','2.12.3'
然后运行捆绑更新链轮
这在其他类似问题中得到了多次回答,如以下问题:
检查浏览器开发工具网络中对模板发出的ajax请求。它们是制造出来的吗?路径正确吗?您提到在脚本标记中显示模板,但没有一个显示得如此清晰,因此服务器上会需要这些模板。对于任何混淆,我深表歉意:在本教程的前一部分中,模板位于main index.html页面的脚本标记中,但这些模板稍后会移动到它们自己的文件中(posts.html和home.html部分)。鉴于app.js位于app/assets/javascripts中,角度路由中模板的路径似乎是正确的(例如“home/_home.html”)。然而,在这一点上,当页面加载时,我没有看到XHR请求,所以这似乎是我无法确定的问题。所有脚本都已成功加载(状态为200),但没有看到Ajax。有一点是确定的……必须从服务器打开页面,而不是从文件打开,因为Ajax受文件协议的限制。应该看到这些模板的XHR请求。控制台中出现任何错误?我确实看到几个“模块'testApp'不可用!您可能拼错了模块名称或忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数”错误。所有模块的模块名都一致为“testApp”,因此至少没有拼写错误。此外,angular rails模板似乎工作正常,因为它正在以脚本的形式加载部分,例如angular.module(“模板”).run([“$templateCache”),function($templateCache){….所以,这似乎归结为那些未捕获的异常。没有什么会跳到我身上。视觉检查语法看起来都很干净。如果模块中有任何东西被破坏,该模块错误将被抛出。还要注意,如果使用angular的缩小版本,您将得到更详细的错误,并使用development VersionTanks!Upvote!堆栈跟踪输出她也很感激。:)仍在努力建立我的代表。