Store Data in a Session : Session « Rails « Ruby






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



 








sessionVariable.zip( 91 k)

Related examples in the same category