1 / 45

Ruby on rails

Ruby on rails. This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on. Where to get rails. Ruby and rails can be downloaded separately, from a variety of sources, in particular RubyForge.

Download Presentation

Ruby on rails

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

  2. Where to get rails • Ruby and rails can be downloaded separately, from a variety of sources, in particular RubyForge. • I downloaded ruby to my desktop so I could conveniently develop applications • I downloaded the “Instant Rails” distribution which comes with Ruby, PHP, and MySQL and the Apache server from https://rubyforge.org/projects/instantrails/ • This simply gets unzipped. Use 7-zip rather than winzip. When you run it you get an interface to start/stop apache and mysql servers. • Not detailed here –you may need to install ruby gems – the ruby package manager. It is available from rubyforge.org.. After extraction, run prompt>ruby setup.rb

  3. bookmarks • Among those you may want to mark are tha rails api pages: http://api.rubyonrails.org/ • The Railspace book has a site at http://railsspace.com/book

  4. Instant Rails looks like this:The I in lefthand corner is clicked to open menus

  5. Remarks on rails • Rails is not directly tied to ruby, and rails apps might be developed in other languages, but rails is written in and for ruby. • Rails is a net programming framework, that builds various directories and files common to an MVC architecture. (There are rails+xyz language available.) • You modify and add to these to customize your development. • Text walkthrough seems mostly accurate. I’ve make some screen shots/notes/etc.

  6. Building a rails app • Select rails application/open ruby console from the “I” • This opens a special DOS window from which ruby on rails (instant rails) is manipulated. • Build a dir… I called mine hello. (The text uses dir name exercises.) • Running the app rails from inside this directory will create subdirectories needed for a rails app.

  7. In DOS window C:\InstantRails\rails_apps>mkdir hello C:\InstantRails\rails_apps>dir Volume in drive C has no label. Volume Serial Number is 2C61-5713 Directory of C:\InstantRails\rails_apps 12/24/2007 12:42 PM <DIR> . 12/24/2007 12:42 PM <DIR> .. 12/22/2007 01:37 PM <DIR> .metadata 12/22/2007 01:37 PM <DIR> cookbook 12/24/2007 12:42 PM <DIR> hello 12/22/2007 01:37 PM <DIR> typo-2.6.0 0 File(s) 0 bytes 6 Dir(s) 62,034,526,208 bytes free C:\InstantRails\rails_apps>cd hello

  8. >rails rails1 C:\InstantRails\rails_apps\hello>rails rails1 create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create components create db create doc create lib create lib/tasks create log create public/images create public/javascripts ….many more here 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:\InstantRails\rails_apps\hello>

  9. Create the controller (named “say”) in the rail1 directory C:\InstantRails\rails_apps\hello>cd rails1 C:\InstantRails\rails_apps\hello\rails1>ruby script/generate controller say exists app/controllers/ exists app/helpers/ create app/views/say exists test/functional/ create app/controllers/say_controller.rb create test/functional/say_controller_test.rb create app/helpers/say_helper.rb C:\InstantRails\rails_apps\hello\rails1>

  10. Inheritance notation • X<Y says class X derives from or subclasses Y. • Example in rails: • SayController < ApplicationController

  11. I modified my SayController adding a method message class SayController < ApplicationController def message end end

  12. rhtml • rhtml is a script file with embedded ruby in an html application

  13. rhtml file • Put file in rails1\app\views\say\message <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <!-- hello.rhtml - the template for Hello World --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Hello, Rails! </title> </head> <body> <h1> Hello from Rails! </h1> </body> </html>

  14. Ruby servers • Ruby comes with webrick and mongrel and you can pick which you wish to run. • Use • script/server webrick to force webrick • Default port is 3000

  15. Run mongrel: ruby script/server from rails1 directory

  16. The app running -note mongrel port #

  17. Delivering dynamic content

  18. Delivering dynamic content <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <!-- hello.rhtml - the template for Hello World --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Hello, Rails! </title> </head> <body> <h1> Hello from Rails! </h1> <p> It is now <%= t =Time.now %><br/> number of sec since midnight= <%= t.hour * 3600 +t.min * 60 + t.sec %><br/> </p> </body> </html>

  19. <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <!-- hello.rhtml - the template for Hello World --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Hello, Rails! </title> </head> <body> <h1> Hello from Rails! </h1> <p> It is now <%= @t %><br/> number of sec since midnight= <%= @tsec %><br/> </p> </body> </html> class SayController < ApplicationController def message @t=Time.now @tsec=@t.hour * 3600 + @t.min * 60 + @t.sec end end Text made a new ap but I revised hello/message

  20. Running, it looks about the same

  21. A form example • This is the form example from the text. • Code is all ok although the directory explanation and other details are a bit off. • A directory popcorn is built below rails_apps but another directory needs to be built below this using rails: I called it popcorn1 • See blackscreen shots for details

  22. A form example

  23. A form example

  24. Blackscreen: mongrel server getting parameters

  25. Text off by one directory level • I built popcorn1 under popcorn under rails_apps. • This was my url: http://localhost:3000/home/the_form • I had to run mongrel (at port 3000) • The form is served via mongrel not apache. (stop the apache server and check the url).

  26. Creating the popcorn1 app C:\InstantRails\rails_apps\popcorn>rails popcorn1 create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create components create db create doc create lib create lib/tasks create log create public/images

  27. Building the controller and running mongrel C:\InstantRails\rails_apps\popcorn>cd popcorn1 C:\InstantRails\rails_apps\popcorn\popcorn1>ruby script/generate controller home exists app/controllers/ exists app/helpers/ create app/views/home exists test/functional/ create app/controllers/home_controller.rb create test/functional/home_controller_test.rb create app/helpers/home_helper.rb C:\InstantRails\rails_apps\popcorn\popcorn1>ruby script/server => Booting Mongrel (use 'script/server webrick' to force WEBrick) => Rails application starting on http://0.0.0.0:3000 => Call with -d to detach • Ctrl-C to shutdown server ** Starting Mongrel listening at 0.0.0.0:3000 ** Starting Rails with development environment... ** Rails loaded. ** Loading any Rails specific GemPlugins ** Signals ready. INT => stop (no restart). ** Mongrel available at 0.0.0.0:3000 ** Use CTRL-C to stop.

  28. Converting a ruby application to a rails application

  29. Summary of steps • Add a directory under rail_apps (I called mine pfix) and change to this directory: Mkdir pfix Cd pfix • Then run rails in this directory to create files and folders using a new subdirectory name, like postfix. • I used the_form.rhtml and result.rhtml files as models for the view. • I used the text’s saycontroller as a model to build my evalcontroller. • Evalcontroller is basically the postfix program. • The stack class needs to be put in this controller directory as well. • I added a variable in the class @the_error and a method errors in the stack class which returns @error, a field value, to the evalcontroller. • The original line, the answer and any error appear in the result form.

  30. After clicking evaluatepostfix button

  31. An expression with errors

  32. Notes • Evalcontroller is in this slide’s notes • Stack class changes left as exercise

  33. The_form for postfix <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <!-- the_form.rhtml This describes a postfix form page> --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Postfix Form </title> </head> <body> <h2> Welcome to postfix evaluator </h2> <!-- The next line gives the address of the CGI program --> <form action = "result" method = "post"> <table> <tr> <td> Enter postfix expression: </td> <td> <input type = "text" name = "expression" size = "70" /> </td> </tr> </table> <p /> <!-- The submit and reset buttons --> <p> <input type = "submit" value = "Evaluate Postfix" /> <input type = "reset" value = "Clear Form" /> </p> </form> </body> </html>

  34. Result.rhtml <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <!-- result.rhtml - result view for the postfix application --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> result.rhtml </title> <head> <body> <table> <tr> <td> postfix expression: </td> <td> <%= @line %> </td> </tr> <tr> <td> Evaluated: </td> <td> <%= @answer %> </td> </tr> <tr> <td> any errors: </td> <td> <%= @the_error %> </td> </tr> </table> </body> </html>

  35. On the DOS window where mongrel is running… Processing EvalController#the_form (for 127.0.0.1 at 2007-12-26 18:25:10) [GET] Session ID: a97b5ba4899036b6dc4239629d8909c3 Parameters: {"action"=>"the_form", "controller"=>"eval"} Rendering eval/the_form Completed in 0.00010 (10000 reqs/sec) | Rendering: 0.00000 (0%) | 200 OK [http:/ /localhost/eval/the_form] form] Processing EvalController#result (for 127.0.0.1 at 2007-12-26 18:25:39) [POST] Session ID: a97b5ba4899036b6dc4239629d8909c3 Parameters: {"action"=>"result", "expression"=>"10 10 20 * + 70 /", "controlle r"=>"eval"} Rendering eval/result Completed in 0.00010 (10000 reqs/sec) | Rendering: 0.00000 (0%) | 200 OK [http:/ /localhost/eval/result] esult]

  36. Access to phpMyAdmin • Click the I • Select configure • Select database

  37. phpMyAdmin in instantrails: go to configure/database from I menu

  38. Unrelated to ruby: accessing mysql from php in instantrails distribution of mysql <?php // Make a MySQL Connection mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); // Retrieve all the data from the "example" table $result = mysql_query("SELECT * FROM Students") or die(mysql_error()); // store the record of the "example" table into $row //$row = mysql_fetch_array( $result ); // Print out the contents of the entry //$result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['name']. " - ". $row['id']; echo "<br />"; } ?>

  39. Note url…put php files in www directory

  40. RadRails • Radrails is a 43 mb download from SourceForge • It is a rails IDE

  41. Radrails looks like this – I wound up not using this environment

  42. Booting mongrel • C:\InstantRails\rails_apps\RailSpace\rail_space>ruby script/server • => Booting Mongrel (use 'script/server webrick' to force WEBrick) • => Rails application starting on http://0.0.0.0:3000 • => Call with -d to detach • => Ctrl-C to shutdown server • ** Starting Mongrel listening at 0.0.0.0:3000 • ** Starting Rails with development environment... • ** Rails loaded. • ** Loading any Rails specific GemPlugins • ** Signals ready. INT => stop (no restart). • ** Mongrel available at 0.0.0.0:3000 • ** Use CTRL-C to stop.

  43. Mongrel runs at localhost:3000

  44. Generating controller for rail_space • C:\InstantRails\rails_apps\RailSpace\rail_space>ruby script/generate controller • Site index about help • exists app/controllers/ • exists app/helpers/ • create app/views/site • exists test/functional/ • create app/controllers/site_controller.rb • create test/functional/site_controller_test.rb • create app/helpers/site_helper.rb • create app/views/site/index.rhtml • create app/views/site/about.rhtml • create app/views/site/help.rhtml • C:\InstantRails\rails_apps\RailSpace\rail_space>

  45. Site_controller.rb class SiteController < ApplicationController def index end def about end def help end end

More Related