Ruby/Rails/View

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Call a Template File Directly

Edit rubydev\double\app\controllers\goto_controller.rb
class HelloController < ApplicationController
  def there
    if Time.now.hour == 12
     render(:file => "C:\here.rhtml")
    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


<A href="http://www.crossplatform.ru/Code/RubyDownload/callTemplateFile.zip">callTemplateFile.zip( 89 k)</a>


Creating a View

// To creates the web application
// Use the command rails applicationName: 
rails crossplatform
//Use the Ruby command ruby script/generate controller Hello:
ruby script/generate controller Hello
//Creating an Action
//Edit hello_controller.rb under crossplatform\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 
//crossplatform\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>
    <br>
    Not bad for a first example!
  </body>
</html>
// To launch your new application:
cd crossplatform
ruby script/server
// In Rails, you create an action by adding a method to a controller, 
// You can reach this new action by navigating to http://localhost:3000/hello/there


<A href="http://www.crossplatform.ru/Code/RubyDownload/firstView.zip">firstView.zip( 87 k)</a>


Pass value from action to view

<A href="http://www.crossplatform.ru/Code/RubyDownload/passValueFromActionToView.zip">passValueFromActionToView.zip( 91 k)</a>

1. <A href="CreatingaView.htm">Creating a View</a> <A href="CreatingaView.htm"></a> 2. <A href="SelectingWhichViewtoRender.htm">Selecting Which View to Render</a> <A href="SelectingWhichViewtoRender.htm"></a> 3. <A href="CallaTemplateFileDirectly.htm">Call a Template File Directly</a> <A href="CallaTemplateFileDirectly.htm"></a>

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


<A href="http://www.crossplatform.ru/Code/RubyDownload/selectViewtoRender.zip">selectViewtoRender.zip( 89 k)</a>