返回顶部

收藏

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

标签:Ruby,Rails

收藏

0人收藏

支持

0

反对

0

发表评论