Aptana studio 3 自动在 rhtml 文件中包含 html 标记

问题描述 投票:0回答:2

我正在使用 aptana studio 3 学习 Ruby on Rails。我创建了一个视图 hello.rhtml。当我运行视图并查看源代码时,我会看到自动包含的 js 文件。即使我的 rhtml 文件为空,例如

hello world

没有 html 标记,如果我看到源代码,我可以看到 html doctype 等已经存在的整个标记。如何停止包含自动标记?

编辑:

我创建了一个 hello.html.erb 文件。并将这段代码放入其中

你好

当我保存并运行文件时,我得到输出,但是当我查看源代码时,我得到以下结果。

<!DOCTYPE html>
<html>
<head>
  <title>Demo</title>
  
  <script src="/javascripts/prototype.js?1312799421" type="text/javascript"></script>
<script src="/javascripts/effects.js?1312799421" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1312799421" type="text/javascript"></script>
<script src="/javascripts/controls.js?1312799421" type="text/javascript"></script>
<script src="/javascripts/rails.js?1312799421" type="text/javascript"></script>

<script src="/javascripts/application.js?1312799421" type="text/javascript"></script>
  <meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="82LBP1pI5h0QzNW54PYSq/zdkS8kF4Z/nKSUHgKvv1g="/>
</head>
<body>

<html>
  <head><title>Hello</title></head>
  <body>
</body>
</html>


</body>
</html>

如您所见,我在 html 中获取 html,并且在浏览器中我的标题显示“Demo”而不是“Hello”

更新:

这里是应用布局的内容。我现在明白了,代码来自这个布局。

<!DOCTYPE html>
<html>
<head>
  <title>Demo</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
</head>
<body>

<%= yield %>

</body>
</html>

最佳实践是什么以及 application_layout.html.erb 中应包含哪些代码?我希望我的所有视图文件都有自己的 html 代码。

html ruby-on-rails aptana
2个回答
1
投票

你的视图应该有 .html.erb 扩展名而不是 rhtml (这是 Rails 1.x 的一个遗留问题)。 检查您的应用程序布局文件。在头部,您将看到包含 javascript 的条目。只要注释掉,你的js就不会被包含在内。

这给我留下了一个问题。你所拥有的实际问题是什么?为什么它让你烦恼?也许这个问题的答案会让您找到正确的问题,以便找到更合适的解决方案。

更新 仔细查看您的 application_layout.html.erb。 可以发一下内容吗?


1
投票

这不是错误,这就是 Rails 的工作方式!

Rails 会为你生成views/layouts/application.html.erb,这是网站的基本模板。使用此模板,您不必在创建的每个页面上复制并粘贴相同的 html 声明。

Rails 使用称为 MVC 的约定,代表模型、视图、控制器。我建议您阅读更多有关其工作原理的内容。基本上, <%= yield %> 将插入视图的内容,但您必须告诉 Rails 哪个 url 指向哪个视图。现在你可以拥有views/pages/home.html.erb,其中的内容可能是..

<h1>Home Page</h1>
<p>Congratulations, you made a web page with Rails!</p>

然后在routes.rb中

YourApp::Application.routes.draw do
  match 'home' => 'pages#home'
end

当您访问 http://localhost:3000/home 时,您应该会看到您的网页。如果您查看源代码,您将看到 Rails 生成的输出。

<!DOCTYPE html>
<html>
<head>
  <title>Demo</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
</head>
<body>
  <h1>Home Page</h1>
  <p>Congratulations, you made a web page with Rails!</p>
</body>
</html>

这绝对是Rails的正确使用方法。目标是 DRY(不要重复自己)。 Michael Hartl 有一篇关于使用 Ruby on Rails 的精彩教程和介绍。我建议任何刚接触 Ruby on Rails 的人从这里开始:http://ruby.railstutorial.org/.

© www.soinside.com 2019 - 2024. All rights reserved.