Ruby/Rails/Action

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

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:59, 13 сентября 2010

Содержание

Create an Application with Two Actions

class HelloController < ApplicationController
  def there
  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/twoActions.zip">twoActions.zip( 88 k)</a>


Display random image

File: app\controllers\hello_controller.rb
class HelloController < ApplicationController
  def show
            @images  = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg"]
            @random_no = rand(5)
            @random_image = @images[@random_no]
    end
end
 
File: app\views\hello\show.rhtml
<html>
<head>
<title>Random Image</title>
</head>
<body>
<h1>Random Image</h1>
Displaying image <%= @random_image%>
<img src="/public/images/<%= @random_image%>">
 
</body>
</html>


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


Link to an Action

class HelloController < ApplicationController
  def there
  end
 
  def here
  end
 
end
 
// File: app\views\hello\there.rhtml:
 <html>
   <head>
     <title>Using Two Views</title>
   </head>
   <body>
     <br>
     <%= link_to "Go to here.", :action => "here" %>
     <br>
     <br>
     This is an active view in a Ruby on Rails application.
   </body>
 </html>
 
 Start the WEBrick server: ruby script/server
 Navigate to http://localhost:3000/hello/there;


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


Passing Data from an Action to a View

//Controller:
class HelloController < ApplicationController
  def there
    @time_now = Time.now
  end
end
 
//View:
 
 <html>
   <head>
     <title>Using Views</title>
   </head>
   <body>
     The time is now <%= @time_now %>
   </body>
 </html>


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