Thursday, May 20, 2010

Workshop 5


Part A: Viewing the action


  1. Create the Rails application framework in the projects folder: C:\InstantRails\...\projects\>rails animals

C:\Ruby>rails animal
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  config/initializers
      create  config/locales
      create  db
      create  doc
      create  lib
      create  lib/tasks
      create  log
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  script/performance
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance
      create  test/unit
      create  vendor
      create  vendor/plugins
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  Rakefile
      create  README
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  config/database.yml
      create  config/routes.rb
      create  config/locales/en.yml
      create  db/seeds.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_rails_defaults.rb
      create  config/initializers/session_store.rb
      create  config/environment.rb
      create  config/boot.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/environments/test.rb
      create  script/about
      create  script/console
      create  script/dbconsole
      create  script/destroy
      create  script/generate
      create  script/runner
      create  script/server
      create  script/plugin
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  test/test_helper.rb
      create  test/performance/browsing_test.rb
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/index.html
      create  public/favicon.ico
      create  public/robots.txt
      create  public/images/rails.png
      create  public/javascripts/prototype.js
      create  public/javascripts/effects.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/controls.js
      create  public/javascripts/application.js
      create  doc/README_FOR_APP
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log


  1. Running  the application on localhost:3000 using the WeBrick ruby server (or Mongrel as alternative) and access via Web browser at http://localhost:3000/




















    3. Create the controller to make the application do an action. This is under the controller-action/model-view structure.

    Stop the WEBrick server each time you edit Ruby classes and then re-start or refresh the views you are testing. Use the Ruby command below:

    >ruby script/generate controller Mammal

    C:\animals>ruby script/generate controller Mammal
          exists  app/controllers/
          exists  app/helpers/
          create  app/views/mammal
          exists  test/functional/
          create  test/unit/helpers/
          create  app/controllers/mammal_controller.rb
          create  test/functional/mammal_controller_test.rb
          create  app/helpers/mammal_helper.rb
          create  test/unit/helpers/mammal_helper_test
    5. Create an action by editing and saving the mammal_controller.rb class in projects\animals\app\controllers using your text editor to add the method below:

    class MammalController< ApplicationController
       def breathe
       end
    end
     









    7. Create and save a view in that directory by using a text editor to create a view called breathe.rhtml

    <html>
                <head>
                   <title>Breathe Easytitle>
                head>
                <body>Inhale and Exhale
                body>
    html>

    8. Try Ruby code and HTML in the action view by using the <%....%> wrapper around the inserted Ruby code. Here are some snippets to try from workshop 4:
    <br>
    5 + 6 =<%= 5 + 6 %>
    br>
    <br>
    =<% 4.times do %>
    Inhale Exhale <br>
    <%end%>
    <br>
    Time is <%=Time.now %>
    br>
     
    Part B: The active view: passing data from an action too a view

    1. Create a new application called scenery in the same projects directory to demonstrate the use of an active view.

    > rails scenery
    > cd scenery

    C:\Ruby>rails scenery
          create
          create  app/controllers
          create  app/helpers
          create  app/models
          create  app/views/layouts
          create  config/environments
          create  config/initializers
          create  config/locales
          create  db
          create  doc
          create  lib
          create  lib/tasks
          create  log
          create  public/images
          create  public/javascripts
          create  public/stylesheets
          create  script/performance
          create  test/fixtures
          create  test/functional
          create  test/integration
          create  test/performance
          create  test/unit
          create  vendor
          create  vendor/plugins
          create  tmp/sessions
          create  tmp/sockets
          create  tmp/cache
          create  tmp/pids
          create  Rakefile
          create  README
          create  app/controllers/application_controller.rb
          create  app/helpers/application_helper.rb
          create  config/database.yml
          create  config/routes.rb
          create  config/locales/en.yml
          create  db/seeds.rb
          create  config/initializers/backtrace_silencers.rb
          create  config/initializers/inflections.rb
          create  config/initializers/mime_types.rb
          create  config/initializers/new_rails_defaults.rb
          create  config/initializers/session_store.rb
          create  config/environment.rb
          create  config/boot.rb
          create  config/environments/production.rb
          create  config/environments/development.rb
          create  config/environments/test.rb
          create  script/about
          create  script/console
          create  script/dbconsole
          create  script/destroy
          create  script/generate
          create  script/runner
          create  script/server
          create  script/plugin
          create  script/performance/benchmarker
          create  script/performance/profiler
          create  test/test_helper.rb
          create  test/performance/browsing_test.rb
          create  public/404.html
          create  public/422.html
          create  public/500.html
          create  public/index.html
          create  public/favicon.ico
          create  public/robots.txt
          create  public/images/rails.png
          create  public/javascripts/prototype.js
          create  public/javascripts/effects.js
          create  public/javascripts/dragdrop.js
          create  public/javascripts/controls.js
          create  public/javascripts/application.js
          create  doc/README_FOR_APP
          create  log/server.log
          create  log/production.log
          create  log/development.log
          create  log/test.log

    C:\Ruby> cd scenery

    C:\Ruby\scenery>
    2. Create a controller called Demo in scenery\app\controllers

    scenery> ruby script/generate controller Demo





















    3. Add an action to demo_controller.rb as the method called rubycobe

    class DemoController< ApplicationController
       def rubycode
       end
    end























     









    4. Add a view template - scenery\app\views\demo\rubycode.rhtml
    We will edit this view in later steps but you may like to add your own test HTML code to the view at this stage.

    5. Save and restart the Web server and navigate to http://localhost:3000/scenery/rubycode
                  
                      Ruby Code
          
    6. Use the Time.now example to pass data from an action to a view. 

     
    7. Modify and save the rubycode action with a value for the time instance variable in the DemoController class in app\controllers\demo_controller.rb

    class DemoController< ApplicationController
       def rubycode
                 @time_now = Time.now
       end
    end

    8. Then modify and save the corresponding view template in \app\views\demo\rubycode.rhtml by adding a call by reference to the action’s instance variable:


    The time is <%= @time.now %>

         

    9. Restart the Web server and navigate the browser to http://localhost:3000/demo/rubycode
                 
               

    Part C: Screen layouts and forms processing with text fields, check boxes, radio buttons and multiple list controls


    1. Create a new application called cabs in the same projects directory to demonstrate the use of an active view.

    > rails cabs
    > cd cabs
    C:\>rails cabs
          create
          create  app/controllers
          create  app/helpers
          create  app/models
          create  app/views/layouts
          create  config/environments
          create  config/initializers
          create  config/locales
          create  db
          create  doc
          create  lib
          create  lib/tasks
          create  log
          create  public/images
          create  public/javascripts
          create  public/stylesheets
          create  script/performance
          create  test/fixtures
          create  test/functional
          create  test/integration
          create  test/performance
          create  test/unit
          create  vendor
          create  vendor/plugins
          create  tmp/sessions
          create  tmp/sockets
          create  tmp/cache
          create  tmp/pids
          create  Rakefile
          create  README
          create  app/controllers/application_controller.rb
          create  app/helpers/application_helper.rb
          create  config/database.yml
          create  config/routes.rb
          create  config/locales/en.yml
          create  db/seeds.rb
          create  config/initializers/backtrace_silencers.rb
          create  config/initializers/inflections.rb
          create  config/initializers/mime_types.rb
          create  config/initializers/new_rails_defaults.rb
          create  config/initializers/session_store.rb
          create  config/environment.rb
          create  config/boot.rb
          create  config/environments/production.rb
          create  config/environments/development.rb
          create  config/environments/test.rb
          create  script/about
          create  script/console
          create  script/dbconsole
          create  script/destroy
          create  script/generate
          create  script/runner
          create  script/server
          create  script/plugin
          create  script/performance/benchmarker
          create  script/performance/profiler
          create  test/test_helper.rb
          create  test/performance/browsing_test.rb
          create  public/404.html
          create  public/422.html
          create  public/500.html
          create  public/index.html
          create  public/favicon.ico
          create  public/robots.txt
          create  public/images/rails.png
          create  public/javascripts/prototype.js
          create  public/javascripts/effects.js
          create  public/javascripts/dragdrop.js
          create  public/javascripts/controls.js
          create  public/javascripts/application.js
          create  doc/README_FOR_APP
          create  log/server.log
          create  log/production.log
          create  log/development.log
          create  log/test.log
    C:\>cd cabs
    C:\cabs>
    2. Create a controller called Vehicle in cabs\app\controllers

    cabs> ruby script/generate controller Vehicle


    3. Add an action to vehicle_controller.rb as the method called cabtype

    class VehicleController< ApplicationController
       def cabtype
       end
    end
    4. Add a view template - cabs\app\views\vehicle\cabtype.rhtml
    We will edit this view in later steps but you may like to add your own test HTML code to the view at this stage.


    6. Create a file in the public directory - \cabs\public called input.html
















    7. Edit the vehicle_controller.rb here is a start. The data in each form element in the Rails application can be accessed via its name and a hash called params

    class VehicleController< ApplicationController
       def cabtype
         @data1 = params[:text1]
         @data2 = params[:check1]
    @data3 = params[:radios1]
    @data4 = params[:building1]
       end
    end






No comments:

Post a Comment