Ruby on Rails页面跳转1.将appcontrollers目录下的say_controller.rb文件的内容改成下面这样:
class SayController
< ApplicationController
def Hello
@time = Time.now
end
def goodby
end
end
我们最后添加了:
def goodby
end
Ruby on Rails页面跳转2.修改appviewssay目录下的hello.rhtml的内容:
<html>
<p>
Say <a href="
/say/goodbye">GoodBye</a>!
</p>
</html>
Ruby on Rails页面跳转3.下面我们要在appviewssay目录下创建一个goodbye.rhtml文件,内容如下:
<html>
<head>
<title>See You Later!</title>
</head>
<body>
<h1>Goodbye!</h1>
<p>
It was nice having you here.
</p>
</body>
</html>
Ruby on Rails页面跳转4.在浏览器地址栏中输入:http://127.0.0.1:3000/say/hello,再试试点链接看看,页面已经可以迁移了。
Ruby on Rails页面跳转5.现在我们再添加由goodbye页面到hello页面的链接,修改goodbye.rhtml的内容为:
<html>
<p>
Say <a href="
/say/hello">Hello</a>!
</p>
</html>
再试试看,两个页面已经可以互相跳转了。
Ruby on Rails页面跳转6.在上面的代码中,我们看到页面间的迁移使用的是路径来定位,如果页面存放的位置发生了变化,那么跳转肯定会失败,更安全的是使用下面的方式。
修改hello.rhtml的内容:
<html>
<head>
<title>Hello, Rails!</title>
</head>
<body>
<h1>Hello from Rails!</h1>
<p>
It is now <%= @time %>.
</p>
<p>
Time to say
<%= link_to "GoodBye!",
:action => "goodbye" %>
</p>
</body>
</html>
修改goodbye.rhtml的内容:
<html>
<head>
<title>See You Later!</title>
</head>
<body>
<h1>Goodbye!</h1>
<p>
It was nice having you here.
</p>
<p>
Say <%= link_to "Hello",
:action=>"hello" %> again.
</p>
</body>
</html>
注意代码里的两句:
<%= link_to "GoodBye!",
:action => "goodbye" %>
<%= link_to "Hello",
:action=>"hello" %>
可以看到,实际上就是调用了goodbye和hello两个action。
OK,Ruby on Rails页面跳转就介绍到这里,先求快速挺进,概念的东西先不追究,希望我能坚持下去。页面跳转实现方法总结
最新推荐文章于 2025-12-18 10:17:25 发布
本文详细介绍了如何在Ruby on Rails中实现页面跳转,包括控制器方法的修改、视图文件的调整以及如何安全地使用链接进行页面迁移。
4175

被折叠的 条评论
为什么被折叠?



