Ruby on rails - Oneonta

quaggahooliganInternet and Web Development

Feb 5, 2013 (4 years and 6 months ago)

124 views

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.


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

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


Instant Rails looks like this:The
I

in lefthand
corner is clicked to open menus

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.

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.

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

>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>




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>

Inheritance notation


X<Y

says class X derives from or
subclasses Y.


Example in rails:


SayController < ApplicationController

I modified my SayController adding
a method message

class SayController < ApplicationController


def message


end

end

rhtml


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

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>


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

Run mongrel: ruby script/server from rails1
directory

The app running
-
note mongrel
port #

Delivering dynamic content

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>


Text made a new ap but I revised
hello/message

<!DOCTYPE html PUBLIC "
-
//w3c//DTD XHTML
1.1//EN"


"http://www.w3.org/TR/xhtml11/DTD/xhtml1
1.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


Running, it looks about the same

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

A form example

A form example

Blackscreen: mongrel server getting
parameters

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).

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

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.


Converting a ruby application to a
rails application

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.

After clicking evaluatepostfix button

An expression with errors

Notes


Evalcontroller is in this slide’s notes


Stack class changes left as exercise

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>


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>


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]


Access to phpMyAdmin


Click the I


Select configure


Select database

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

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 />";

}

?>



Note url…put php files in www directory

RadRails


Radrails is a 43 mb download from
SourceForge


It is a rails IDE

Radrails looks like this


I wound
up not using this environment

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.

Mongrel runs at localhost:3000

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>

Site_controller.rb

class SiteController < ApplicationController



def index


end



def about


end



def help


end

end