Understanding Action Views in Ruby-on-Rails - Rails in the Real World
(Page 4 of 4 )
Let's take the simple application that was created in the last part a little further. To jog your memory here is the code for the Controller:
class SayController < ApplicationController
defhello
@time=Time.now
end
end
and for the View:
<html>
<head>
<title>Hello, Rails!</title>
</head>
<body>
<h1>Hello from Rails! The time now is<%=@time%></h1>
</body>
</html>
Now let's add a string and float variable to the controller.
class SayController < ApplicationController
def hello
@time=Time.now
@rule=”All sentence should have a starting, a
mid section and an end except”
@price=1000.123
end
end
In the View let's use the formatting helpers (the view is already a template):
<html>
<head>
<title>Hello, Rails!</title>
</head>
<body>
<br>The time now is<%=@time%>
<br>The time now in words<%= distance_of_time_in_words(@time, Time.local(2005, 12, 25)) %>
<br>The highlight works like this<%= highlight(@rule, "sentence") %>
<br>The price 1000.123 in Canadian dollar is
<%= number_to_currency(@price, :unit => "CAN$", :precision => 2) %>
</body>
</html>
That’s all for this part. In the next part the last piece of the puzzle, i.e. the Model component, will be discussed. Once that is completed, I will start to discuss all the components in relation to each other using a project. Till then…
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |