为什么我的链接对我在Rails网站上的红宝石不起作用?链接在单击时保持在同一页面上 [英] Why are my links not working for my ruby on rails site? Links stay on same page when clicked on
问题描述
我的链接对我在Rails网站上的红宝石无效.当我单击链接时,网站停留在同一页面上.我正在阅读一本书的代码,该书是:"RailsSpace:用红宝石构建一个社交网站",作者是迈克尔·哈特尔(Michael Hartl).该书可免费下载,您可以自己遵循代码,第55-56页的标题为添加导航".这是电子书的链接.
My links aren't working for my ruby on rails site. When I click the link the site stays on the same page. I'm following code from a book called: "RailsSpace: Building a social networking site with ruby on rails" By Michael Hartl. The book is available free for download you can follow the code for yourself, its on pages 55-56 starting under the title "adding navigation". Here is a link to the ebook.
感谢您的帮助.
我尝试使用谷歌搜索"link_to"函数并更改格式,但这样做只会产生更多错误,请帮忙.
I tried googling the "link_to" function and changing the format, all it did was generate more errors please help.
这是我的app/views/layout/site.html.erb文件中的代码:
Here is the code from my app/views/layout/site.html.erb file:
<!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文件的代码:
Here is the code for my app/controllers/site_controller.rb file:
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文件的代码:
Here is the code for my config/routes.rb file:
Rails.application.routes.draw do
get 'site/index'
get '/site/about'
get 'site/help'
root 'site#index'
//这是我按要求单击链接时控制台的输出:
// Here is the output from the console when I click on a link as requested:
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)
感谢您的所有帮助
当我单击导航栏中的链接时,该网站将停留在同一页面上.请帮忙.
When I click on the links in my navbar the site stays on the same page. Please help.
推荐答案
感谢罗克韦尔·赖斯先生,他建议我将<%= @content_for_layout %>
替换为<%= yield %>
,它确实有效.所有人的称赞都是洛克威尔·赖斯先生.非常感谢男人.
Thanks To Mr. Rockwell Rice, he suggested that I replace <%= @content_for_layout %>
with <%= yield %>
and it actually worked. All praise goes to Mr. Rockwell Rice. Thanks alot man.
这篇关于为什么我的链接对我在Rails网站上的红宝石不起作用?链接在单击时保持在同一页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!