首页 > 解决方案 > 学习 Rails - 路线在我的第一个教程中不起作用

问题描述

我希望每个人都过得愉快。在追求知识的过程中,我遇到了一个巨大的绊脚石。

我正在关注本教程。我正在使用最新的一切,我认为他在 Rails 4 中,我在 Rails 5 中。

https://youtu.be/GY7Ps8fqGdc?t=703

观看教程绝不是理解我的问题所必需的。在我链接的地方,除了生成一个简单的 rails 应用程序之外,他唯一做的就是生成一个像这样的控制器:

rails generate controller welcome index

然后就在我链接到视频的地方,他进入路由文件并取消注释我的 rails 应用程序中不存在的行,所以我只是手动编写的

root 'welcome#index'

当我尝试加载 localhost:3000 时它失败,抛出一个 jsexception “TypeError:Object 不支持此属性或方法”

是否有一些功能已被弃用或什么?为什么这不起作用?我已经完成了至少 5 次链接的教程。我已经非常努力地解决了这个问题,但无济于事。

哦,在有人告诉我你不应该那样路由之前,我之前已经读过,但他只是想让观众能够在网页上执行 ruby​​ 代码,他稍后会详细介绍路由,但我不能不打败这个就到达那里,因为课程依赖于我首先得到这个。

请,哦,很棒的论坛,请给我一些答案,我会给你一个支持和非常感谢的东西。

另外,附带说明一下,如果有人可以推荐一些学习资源,这些学习资源可以帮助您通过这个框架的初学者,除了回答这个问题之外,我很乐意接受建议。

编辑:包括要求的信息

routes.rb 文件

Rails.application.routes.draw do
  get 'sausage/tuna'
  root 'sausage#tuna'
end

错误输出

最后一条信息,可能很明显,我称我的东西很有趣,所以通用的“欢迎”和“索引”不会让我感到困惑。我从新波士顿汲取灵感,并称我的“香肠金枪鱼”而不是“欢迎指数”。

再次感谢您帮助我。我真的很想学这个。

标签: ruby-on-railsrubyruby-on-rails-5

解决方案


好的,问题似乎来自 ExecJS,它基本上是在 Ruby 中处理 JS 的 gem。

违规行是:(<%= stylesheet_link_tag 'application', media: 'all' %> 如您所见,我缺少 turbolinks 位,因为我没有使用 turbolinks)

这意味着什么?基本上,这一行用于附加到此页面的专用 JS 代码表。

这段代码在哪里?application.html.erb. application.html.erb是默认视图布局。这意味着您的代码触发的每个视图都继承自这个“主”视图。

所以如果错误进入这个阶段,基本上你的路由是好的。你的 Sausage#tuna 动作被触发,然后 `tuna.html.erb" 视图被渲染,但在这个主视图上出现错误。

基本上因为你有一个错误application.html.erb,任何控制器的每个动作和触发的每个视图都会得到相同的错误。

现在有什么问题?好吧,我从来没有遇到过这个错误,但不喝咖啡似乎可以解决你的问题。虽然在我的情况下,我的gem 'coffee-rails'一些 JS 文件实际上是“coffee.erb”文件,但我没有得到同样的错误。所以我想它比这更复杂..

但是为了让你更好地理解 Rails,正如我之前所说,这一行是编译一些 JS 并将其附加到主视图文件(然后附加到应用程序的每个视图)

现在如果你想知道什么 JS 将被编译到这个单独的表格中,请查看文件app/assets/javascrips/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/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery3
//= require jquery_ujs
//= require bootstrap
//= require cable
//= require rails.validations
//= require footer.js
//= require serviceworker-companion

一切看起来都被注释掉了,但实际上最后几行//= require实际上是从不同的 gem 中收集了一些 JS。(jQuery,...)

就我而言:Jquery、Bootstrap、actioncable .. 这是我在应用程序的每个页面上都需要的 Js ......

关于你关于一个好的教程的问题,我更喜欢 Rails 网站上的入门教程:https ://guides.rubyonrails.org/getting_started.html

编辑

我的错。造成问题的是样式表。我更不确定是什么原因造成的,因为 ExecJS 正在处理您的应用程序中的 Js 代码,而不是 CSS。样式表的工作方式与上面所说的完全相同application.js。它将一些 CSS 样式表链接到您的每个应用程序页面。

这是我的app/assets/stylesheets/application.css

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 *= require navbars.css.scss
 *= require footer.css.scss
 *= require cookiewarning.css.scss
 *= require_self
 */

@import "bootstrap-sprockets";
@import "bootstrap";

如您所见,在我的应用程序的每个页面上都附加了导航栏 CSS 和页脚 CSS 以及一些其他 CSS。

(感谢您接受我的答案作为答案,但绝对不是,只是想向您解释一下在做什么:))


推荐阅读