Ruby/Rails/Session

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

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

Store Data in a Session

File: controllers\hello_controller.rb
class HelloController < ApplicationController
  def there
    @counter1 = 1
    if(session[:counter2])
      @counter2 = session[:counter2]
      @counter2 += 1
      session[:counter2] = @counter2
    else
      @counter2 = 1
      session[:counter2] = @counter2
    end
  end
end
 
File: app\views\hello\there.rhtml
<html>
  <head>
    <title>Using Sessions</title>
  </head>
  <body>
    <h1>Working With Sessions</h1>
    This Ruby on Rails application lets you store data in sessions.
    <%= form_tag ({:action => "at"}, {:method => "post"}) %>
      <br>
      Counter 1: <%= @counter1 %>.
      <br>
      Counter 2: <%= @counter2 %>.
      <br>
      <br>
    <%= form_tag %>
  </body>
</html>
 
Start the WEBrick server: ruby script/server
Navigate to http://localhost:3000/hello/there


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