ROR学习系列20-Rails基础3-创建第二个Action

我们创建新的rails工程 double,创建Goto控制器。
对其定义如下:
class GotoController < ApplicationController
def work
end
def lunch
end
end

在\app\views\goto中定义work.rhtml:
<html>
<head>
<title>Using Two Views</title>
</head>
<body>
<h1>Working With Two Views</h1>
<br>
<br>
<h1>Get back to work!</h1>
<br>
<br>
This is an active view in a Ruby on Rails application.
</body>
</html>

在\app\views\goto中定义lunch.rhtml:
<html>
<head>
<title>Using Two Views</title>
</head>
<body>
<h1>Working With Two Views</h1>
<br>
<br>
<h1>Lunch time!</h1>
<br>
<br>
This is an active view in a Ruby on Rails application.
</body>
</html>

下面我们修改控制器代码如下:
class GotoController < ApplicationController
def work
if Time.now.hour == 12
render(:action => :lunch)
end
end
def lunch
end
end

由控制器来显示那一个页面。
注意这里有render(:action => :lunch),它把action转向了另一个。
我们还可以再改一下控制器代码:
class GotoController < ApplicationController
def work
if Time.now.hour == 12
render(:file => ‘C:\rubydev\ch04\double\app\views\goto\lunch.rhtml’)
end
end


下面我们再看一下work.rhtml代码:
<html>
<head>
<title>Using Two Views</title>
</head>
<body>
<h1>Working With Two Views</h1>
<br>
<br>
<h1>Get back to work!</h1>
<br>
<br>
<%= link_to “Go to lunch.”, :action => “lunch” %>
<br>
<br>
This is an active view in a Ruby on Rails application.
</body>
</html>


在这里就相当于:
<a href=”/goto/lunch”>Go to lunch.</a>

你可能感兴趣的:(html,c,Ruby,Rails,Go)