by http://webgeektutorials.blogspot.com

Thursday, June 16, 2011

View in Ruby

Use the Ruby command ruby script/generate controller Hello:
ruby script/generate controller Hello

Creating an Action
//Edit hello_controller.rb under \app\controllers

class HelloController < ApplicationController
                 def there...............
end
end

//To establish a view template for the hello controller's there action, you can create a file named there.rhtml and store it in the
//\app\views\hello directory.
<html>
<head>
<title>Using Ruby on Rails</title>
</head>
<body>
<h1>Welcome to Ruby on Rails</h1>
This is your first Ruby on Rails application.
<br>
<br>
Using this application, you've been introduced to
controllers, actions, and views.
<br>
  </body>
</html>

// To launch your new application:
ruby script/server

Selecting Which View to Render
class HelloController < ApplicationController
def there
if Time.now.hour == 12
render(:action => :here)
end
end
        def here
  end
end

// File: app\views\hello\there.rhtml:
<html>
<head>
<title>Using Two Views</title>
</head>
<body>
there
</body>
</html>

//File: app\views\hello\here.rhtml:
<html>
<head>
<title>Using Two Views</title>
</head>
<body>
   here
</body>
</html>

Start server: ruby script/server
Navigate to http://localhost:3000/hello/there
Now navigate to http://localhost:3000/hello/here

No comments:

Post a Comment