【发布时间】:2016-03-15 05:55:36
【问题描述】:
我正在学习 AngularJS 以及如何将它与 Rails 一起使用,所以我开始关注this 这本书,第 1 章中的所有内容都运行良好...这包括安装必要的 gem、安装 bower、通过 bower 安装依赖项、设置资产路径等...即使Tiniest Angular app ever 也能完美运行。
我在按照第 2 章的说明进行操作时遇到了问题。
假设我在访问我的应用程序时应该看到this,但我看到的不是this,而是一个空白页面...
终端上显示的完整错误是这样的
ActionController::RoutingError (No route matches [GET] "/index.html"):
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/patricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/patricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/patricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.1ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (1.8ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (7.6ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (39.5ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.4ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.5ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (15.3ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (29.7ms)
我不明白为什么会发生这种情况,到目前为止我还没有解决这个问题。
我从第 1 章到第 2 章中出现问题的地方所做的唯一更改是:
- 将
gem 'angular-rails-templates'添加到 Gemfile。 - 运行
bundle install。 - 将
asset 'angular-route'添加到 Bowerfile。 - 运行
rake bower:install。 - 将
//= require angular-route/angular-route和//= require angular-rails-templates添加到/assets/javascripts/application.js 文件。
最后将下面的代码放入他们的文件中,就像书中提到的那样。
app/views/home/index.html.erb
<div ng-app="receta">
<div class="view-container">
<div ng-view class="view-frame animate-view"></div>
</div>
</div>
app/assets/javascripts/app.coffee
receta = angular.module('receta',[
'templates',
'ngRoute',
'controllers',
])
receta.config([ '$routeProvider',
($routeProvider)->
$routeProvider
.when('/',
templateUrl: "index.html"
controller: 'RecipesController'
)
])
controllers = angular.module('controllers',[])
controllers.controller("RecipesController", [ '$scope',
($scope)->
])
app/assets/javascripts/templates/index.html
<header class="row">
<h1 class="text-center col-md-6 col-md-offset-3">Find Recipes</h1>
</header>
<section class="row">
<form>
<div class="form-group col-md-6 col-md-offset-3">
<label for="keywords" class="sr-only">Keywords</label>
<input type="text" autofocus class="form-control" placeholder="Recipe name, e.g. Baked Potato">
</div>
<div class="form-group col-md-6 col-md-offset-3 text-center">
<button class="btn btn-primary btn-lg">Search</button>
</div>
</form>
</section>
<hr>
<section class="row">
<h1 class="text-center h2">Results</h1>
<section class="well col-md-6 col-md-offset-3">
<h1 class="h3 col-md-6 text-right" style="margin-top: 0"><a href="#">Baked Potato w/ Cheese</a></h1>
<div class="col-md-6">
<button class="btn btn-info">Edit</button>
<button class="btn btn-danger">Delete</button>
</div>
</section>
<section class="well col-md-6 col-md-offset-3">
<h1 class="h3 col-md-6 text-right" style="margin-top: 0"><a href="#">Garlic Mashed Potatoes</a></h1>
<div class="col-md-6">
<button class="btn btn-info">Edit</button>
<button class="btn btn-danger">Delete</button>
</div>
</section>
<section class="well col-md-6 col-md-offset-3">
<h1 class="h3 col-md-6 text-right" style="margin-top: 0"><a href="#">Potatos Au Gratin</a></h1>
<div class="col-md-6">
<button class="btn btn-info">Edit</button>
<button class="btn btn-danger">Delete</button>
</div>
</section>
</section>
This 是我正在尝试做的应用程序的 github 存储库(带有相应的提交),如果你想查看其他文件,我真的不知道我应该提供什么其他信息,因为我'我是 AngularJS 的新手以及如何在 Rails 中使用它。
编辑:
rake routes
Prefix Verb URI Pattern Controller#Action
home_index GET /home/index(.:format) home#index
root GET / home#index
app/assets/javascripts/application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require angular/angular
//= require angular-route/angular-route
//= require angular-rails-templates
//= require_tree ./templates
//= require_tree
Bowerfile
asset 'angular'
asset 'angular-route'
asset 'bootstrap-sass-official'
# vim: ft=ruby
【问题讨论】:
-
您是否在
config/routes.rb中为您的root_path定义了路由?例如。root :to => 'home#index' -
@dinjas 是的,我愿意...
-
您是否已将
require_tree ./templates添加到您的application.js 中? -
@BroiSatse 我没有,但我只是添加了
// = require_tree ./templates并且没有任何变化。 -
@dinjas - 这不是路由错误。 'rails-angular-templates' 应该将所有模板添加到角度缓存中 - 当一切正确时,根本就不应该有任何请求。
标签: ruby-on-rails angularjs ruby-on-rails-4 routes angular-ui-router