学习Rails-在我的第一个教程中无法使用的路线-新手

时间:2018-11-10 21:09:42

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

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

我正在关注本教程。我正在使用最新的东西,我认为他在第4栏上,而我在第5栏上。

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

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

rails generate controller welcome index

然后在我链接到视频的地方,他进入了routes文件并取消注释了我的rails应用程序中不存在的一行,因此我只是手动编写了

root 'welcome#index'

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

是否已弃用某些功能?为什么这不起作用?我已经完成了本教程,直到至少链接了5次。我已经竭尽全力解决这个问题,但无济于事。

哦,在有人告诉我你不应该这样路由之前,我已经读过了,但是他只是想让观看者达到可以在网页上执行ruby代码的地步,他介绍了路由稍后再详细介绍,但是我无法击败它,因为这堂课依赖于我首先获得。

请哦,哦,好的论坛,请给我一些答案,我将给您赞美和感谢。

此外,如果有人可以推荐一些学习资源,帮助您超越该框架的初学者,那么除了回答这个问题外,我很乐意接受建议。

编辑:包括所请求的信息

routes.rb文件

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

the error output

最后一条信息,可能很明显,我称我的事情为有趣的名字,因此通用的“欢迎”和“索引”不会使我感到困惑。我从新波士顿汲取了灵感,并将其称为“香肠金枪鱼”,而不是“欢迎指数”。

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

2 个答案:

答案 0 :(得分:1)

好吧,问题似乎出在ExecJS上,它基本上是在Ruby中处理JS的瑰宝。

违规行是:<%= stylesheet_link_tag 'application', media: 'all' %> (如您所见,由于我没有使用涡轮链接,我缺少了涡轮链接)

这是什么意思?基本上,此行是此页面所附的专用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。

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

答案 1 :(得分:0)

我找到了解决该问题的可靠方法。我安装了node.js,现在可以正常使用咖啡了。