Working with Models : Model « Rails « Ruby






Working with Models


File: app\models\cruncher.rb

class Cruncher
  def crunch
    return 5
  end
end

File: app\controllers\hello_controller.rb

class HelloController < ApplicationController
  def there
    @cruncher = Cruncher.new
    @data = @cruncher.crunch
  end
end

File: app\views\hello\there.rhtml:

<html>
  <head>
    <title>Using Models</title>
  </head>
  <body>
    <h1>Working With Models</h1>
    This application fetches data from a model.
    <br>
    <br>
    The fetched data is: <%= @data %>.
    <br>

    <br>
  </body>
</html>


Start the WEBrick server: ruby script/server
Navigate to http://localhost:3000/hello/there



 








workWithModel.zip( 90 k)

Related examples in the same category

1.Tie a Text Field to a Model
2.Initialize Data in an HTML Control