我的链接对我在Rails网站上的红宝石无效。当我单击链接时,网站停留在同一页面上。我正在阅读一本书的代码,该书是:“ RailsSpace:用红宝石构建一个社交网站”,作者是Michael Hartl。该书可免费下载,您可以自己遵循代码,第55-56页的标题为“添加导航”。这是ebook的链接。
感谢您的帮助。
我尝试使用谷歌搜索“ link_to”函数并更改格式,所做的只是产生更多错误,请帮助。
这是我的app / views / layout / site.html.erb文件中的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><%= @title %></title>
<%= stylesheet_link_tag "site" %>
</head>
<body>
<div id="whole_page">
<div id="header">RailsSpace</div>
<div id="nav">
<%= link_to_unless_current "Home", :action => "index" %> |
<%= link_to "About Us" , :action => "help" %> |
<%= link_to_unless_current "Help", :action => "help" %>
<div style="background-color:white">
<h1>Welcome to RailSpace!</h1>
<p>This is going to be the best site ever!</p>
</div>
</div>
<div id="content">
<%= @content_for_layout %>
</div>
</div>
</body>
</html>
这是我的app / controllers / site_controller.rb文件的代码:
class SiteController < ApplicationController
def index
@title = "Welcome to RailsSpace!"
end
def about
@title = "About RailsSpace"
@header = "Hello"
end
def help
@title = "RailsSpace Help"
end
end
这是我的config / routes.rb文件的代码:
Rails.application.routes.draw do
get 'site/index'
get '/site/about'
get 'site/help'
root 'site#index'
//这是我按要求单击链接时控制台的输出:
Started GET "/site/about" for ::1 at 2019-10-01 15:28:48 -0700
Processing by SiteController#about as HTML
Rendering site/about.html.erb within layouts/site
Rendered site/about.html.erb within layouts/site (Duration: 0.0ms |
Allocations: 5)
Completed 200 OK in 7ms (Views: 6.8ms | ActiveRecord: 0.0ms |
Allocations: 2921)
感谢您的所有帮助
当我单击导航栏中的链接时,该网站将停留在同一页面上。请帮助。
[感谢Rockwell Rice先生,他建议我将<%= @content_for_layout %>
替换为<%= yield %>
,但它确实有效。所有人的称赞都是洛克威尔·赖斯先生。非常感谢男人。