Part A: Viewing the action
- 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
- 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_test5. 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< ApplicationControllerdef breatheendend
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 Exhalebody>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 scenerycreatecreate app/controllerscreate app/helperscreate app/modelscreate app/views/layoutscreate config/environmentscreate config/initializerscreate config/localescreate dbcreate doccreate libcreate lib/taskscreate logcreate public/imagescreate public/javascriptscreate public/stylesheetscreate script/performancecreate test/fixturescreate test/functionalcreate test/integrationcreate test/performancecreate test/unitcreate vendorcreate vendor/pluginscreate tmp/sessionscreate tmp/socketscreate tmp/cachecreate tmp/pidscreate Rakefilecreate READMEcreate app/controllers/application_controller.rbcreate app/helpers/application_helper.rbcreate config/database.ymlcreate config/routes.rbcreate config/locales/en.ymlcreate db/seeds.rbcreate config/initializers/backtrace_silencers.rbcreate config/initializers/inflections.rbcreate config/initializers/mime_types.rbcreate config/initializers/new_rails_defaults.rbcreate config/initializers/session_store.rbcreate config/environment.rbcreate config/boot.rbcreate config/environments/production.rbcreate config/environments/development.rbcreate config/environments/test.rbcreate script/aboutcreate script/consolecreate script/dbconsolecreate script/destroycreate script/generatecreate script/runnercreate script/servercreate script/plugincreate script/performance/benchmarkercreate script/performance/profilercreate test/test_helper.rbcreate test/performance/browsing_test.rbcreate public/404.htmlcreate public/422.htmlcreate public/500.htmlcreate public/index.htmlcreate public/favicon.icocreate public/robots.txtcreate public/images/rails.pngcreate public/javascripts/prototype.jscreate public/javascripts/effects.jscreate public/javascripts/dragdrop.jscreate public/javascripts/controls.jscreate public/javascripts/application.jscreate doc/README_FOR_APPcreate log/server.logcreate log/production.logcreate log/development.logcreate 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< ApplicationControllerdef rubycodeendend
4. Add a view template - scenery\app\views\demo\rubycode.rhtmlWe 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. 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< ApplicationControllerdef rubycode@time_now = Time.nowendend
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 cabsC:\>rails cabscreatecreate app/controllerscreate app/helperscreate app/modelscreate app/views/layoutscreate config/environmentscreate config/initializerscreate config/localescreate dbcreate doccreate libcreate lib/taskscreate logcreate public/imagescreate public/javascriptscreate public/stylesheetscreate script/performancecreate test/fixturescreate test/functionalcreate test/integrationcreate test/performancecreate test/unitcreate vendorcreate vendor/pluginscreate tmp/sessionscreate tmp/socketscreate tmp/cachecreate tmp/pidscreate Rakefilecreate READMEcreate app/controllers/application_controller.rbcreate app/helpers/application_helper.rbcreate config/database.ymlcreate config/routes.rbcreate config/locales/en.ymlcreate db/seeds.rbcreate config/initializers/backtrace_silencers.rbcreate config/initializers/inflections.rbcreate config/initializers/mime_types.rbcreate config/initializers/new_rails_defaults.rbcreate config/initializers/session_store.rbcreate config/environment.rbcreate config/boot.rbcreate config/environments/production.rbcreate config/environments/development.rbcreate config/environments/test.rbcreate script/aboutcreate script/consolecreate script/dbconsolecreate script/destroycreate script/generatecreate script/runnercreate script/servercreate script/plugincreate script/performance/benchmarkercreate script/performance/profilercreate test/test_helper.rbcreate test/performance/browsing_test.rbcreate public/404.htmlcreate public/422.htmlcreate public/500.htmlcreate public/index.htmlcreate public/favicon.icocreate public/robots.txtcreate public/images/rails.pngcreate public/javascripts/prototype.jscreate public/javascripts/effects.jscreate public/javascripts/dragdrop.jscreate public/javascripts/controls.jscreate public/javascripts/application.jscreate doc/README_FOR_APPcreate log/server.logcreate log/production.logcreate log/development.logcreate log/test.logC:\>cd cabsC:\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< ApplicationControllerdef cabtypeendend4. Add a view template - cabs\app\views\vehicle\cabtype.rhtmlWe 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< ApplicationControllerdef cabtype@data1 = params[:text1]@data2 = params[:check1]@data3 = params[:radios1]@data4 = params[:building1]endend
No comments:
Post a Comment